【问题标题】:Change Shortcode wrapper in Woocommerce在 Woocommerce 中更改简码包装器
【发布时间】:2014-02-24 07:21:44
【问题描述】:

我正在使用 Wordpress 3.8 + Woocommerce 2.0 当我使用简码时,我需要更改 Woocommerce 生成的包装器的类。

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

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

我要获得

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

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

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

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

【问题讨论】:

    标签: wrapper woocommerce shortcode


    【解决方案1】:

    您可以创建自己的简码,只是默认简码的克隆,但要进行更改,因此请将其粘贴到您的 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');
    

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

    这将创建一个新的短代码,与“recent_products”几乎相同,但只有变化。

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

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

    或在您的帖子中:

     [custom_recent_products per_page="3"]
    

    我不知道这是否是最好的方法,但就我所见,“woocommerce”类直接在recent_products 短代码函数上作为html 返回,所以我无法想象如何过滤或挂钩换个方式。

    希望对我有帮助,对不起我的英语不好:)

    【讨论】:

    • 感谢您的回答,这可能是一种方法,但我不想为客户将在网站中使用的每种短代码制作自定义功能。我认为有一种扩展 shortcode_wrapper 函数的方法...但是我关于 php 编程的新手状态不允许我找到解决方案!
    • 但这里的问题是我相信默认类“woocommerce”的“包装器”不能以另一种方式过滤,只需查看该类:return '
      '。 ob_get_clean() 。 '
      ';我的意思是在 shortcode_wrapper 上没有定义。
    • 顺便说一下,你可以把默认的短代码放在一个 div 中,比如说“my_recent_products”类,这样你就可以拥有 .my_recent_products > .woocommerce,这样你就可以覆盖 css样式,如果这是你需要的。
    • 你是对的,但所有这些解决方案都是变通方法......我相信可能有直接的方法
    【解决方案2】:
    you can use following trick to over right wrapper function :
    Here i have created a shortcode for checkout and shortcode and change wrapper
    shortcode will be [overright_checkout_of_shortcode] 
    function shortcode_handler($atts) {
        $classget=new WC_Shortcodes();
        $wrapper = array(
                'class'  => '',
                'before' => '<div id="accordion" class="panel-group faq_group checkout-group" role="tablist" aria-multiselectable="true" >',
                'after'  => '</div>'
            );
      return $classget->shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ,$wrapper);
     }
    add_shortcode('overright_checkout_of_shortcode','shortcode_handler');
    

    【讨论】:

    • 完美运行!我不得不对其进行一些更改,以便在我的帐户页面下将其他 css 类添加到 &lt;div class="woocommerce"&gt; 包装器。后来我只是在管理页面下更新/保存了我的帐户的默认 WooCommerce 页面,删除了 [woocommerce_my_account] 短代码并替换为新创建的。
    • @AdrianoMonecchi (y)
    猜你喜欢
    • 2016-12-12
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多