在 Woocommerce 中更改短代码包装器 [英] Change Shortcode wrapper in Woocommerce

查看:20
本文介绍了在 Woocommerce 中更改短代码包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Wordpress 3.8 + Woocommerce 2.0当我使用短代码时,我需要更改 Woocommerce 生成的包装器的类.

我使用这个简码:[recent_products per_page="12"]输出是:

the_product_loop....

我要获得

the_product_loop....

但是我找不到我必须更改代码的地方...在 class-wc-shortcodes.php 文件中,我找到了生成包装器的函数的声明:

公共静态函数shortcode_wrapper($函数,$atts = array(),$wrapper = 数组('类' =>'woocommerce','之前' =>空值,'之后' =>空值))

但是...我不想改变Woocommerce插件的核心文件,可以通过functions.php定义我的自定义类??

解决方案

您可以创建自己的短代码,只是默认短代码的克隆,但进行更改后,请将其粘贴到您的 functions.php 中:

function custom_recent_products_FX($atts) {全球 $woocommerce_loop, $woocommerce;提取(shortcode_atts(数组('per_page' =>'12','列' =>'4','orderby' =>'日期','订单' =>'降'), $atts));$meta_query = $woocommerce->query->get_meta_query();$args = 数组('post_type' =>'产品','post_status' =>'发布','ignore_sticky_posts' =>1、'posts_per_page' =>$per_page,'orderby' =>$orderby,'订单' =>$订单,'meta_query' =>$meta_query);ob_start();$products = new WP_Query( $args );$woocommerce_loop['columns'] = $columns;如果 ( $products->have_posts() ) : ?><?php woocommerce_product_loop_start();?><?php while ( $products->have_posts() ) : $products->the_post();?><?php woocommerce_get_template_part('content', 'product');?><?php endwhile;//循环结束.?><?php woocommerce_product_loop_end();?><?php endif;wp_reset_postdata();return '

'.ob_get_clean() .'</div>';}add_shortcode('custom_recent_products','custom_recent_products_FX');

请注意该函数末尾的MY_CUSTOM_CLASS",根据您的需要进行更改.

这将创建一个新的短代码,与recent_products"几乎相同,但只有一点变化.

所以要输出这个,只需在模板上使用:

 echo do_shortcode('[custom_recent_products per_page="3"]');

或在您的帖子中:

 [custom_recent_products per_page="3"]

我不知道这是否是最好的方法,但就我所见,woocommerce"类直接在最近的_products 短代码函数上作为 html 返回,所以我无法想象如何过滤或钩住它顺便说一句.

希望能帮到你,对不起我的英语不好:)

I'm using Wordpress 3.8 + Woocommerce 2.0 I need to change the class of the wrapper that Woocommerce generate when I use a shortcode.

I use this shortcode: [recent_products per_page="12"] And the output is:

<div class="woocommerce">
   the_product_loop....
</div>

I want to obtain

<div class="MYCUSTOMCLASS">
   the_product_loop....
</div>

But I can't find where I have to change the code... In class-wc-shortcodes.php file I've found the declaration of the function that generates the wrapper:

public static function shortcode_wrapper(
    $function,
    $atts    = array(),
    $wrapper = array(
        'class'  => 'woocommerce',
        'before' => null,
        'after'  => null
    )
)

But... I don't want to change the core files of Woocommerce plugin, It's possible to define my custom class via functions.php??

解决方案

You can create your own shortcode, just a clone of the default one, but with that change, so paste this in your functions.php:

function custom_recent_products_FX($atts) {
    global $woocommerce_loop, $woocommerce;

    extract(shortcode_atts(array(
        'per_page'  => '12',
        'columns'   => '4',
        'orderby' => 'date',
        'order' => 'desc'
    ), $atts));

    $meta_query = $woocommerce->query->get_meta_query();

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => $per_page,
        'orderby' => $orderby,
        'order' => $order,
        'meta_query' => $meta_query
    );

    ob_start();

    $products = new WP_Query( $args );

    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php woocommerce_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    <?php endif;

    wp_reset_postdata();

    return '<div class="MY_CUSTOM_CLASS">' . ob_get_clean() . '</div>';
 }
 add_shortcode('custom_recent_products','custom_recent_products_FX');

Notice at the end of that function the "MY_CUSTOM_CLASS", change that for your needs.

This will create a new shortcode, almost same than the "recent_products" one but with that only change.

So to output this, just use on template:

 echo do_shortcode('[custom_recent_products per_page="3"]');

Or in your posts:

 [custom_recent_products per_page="3"]

I don´t know if this is the best approach, but for what i can see, the class "woocommerce" is returned directly on the recent_products shortcode function as html, so i can´t imagine how to filter or hook this by anotherway.

Hope that helps and sorry my bad english :)

这篇关于在 Woocommerce 中更改短代码包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆