【问题标题】:Extend WooCommerce Product Filters plugin shortocodes扩展 WooCommerce 产品过滤器插件短代码
【发布时间】:2021-07-24 13:45:48
【问题描述】:

我正在使用带有 WOOF 插件(woocommerce 过滤器)的 Woocommerce。特别是,此插件可以显示一个过滤器,该过滤器将使用例如 [woof taxonomies=product_cat:23] 短代码仅在特定产品类别中搜索,并使用 [woof_products taxonomies=product_cat:23] 短代码显示结果,其中 23 是类别商品编号。

但是,并非总是可以在简码本身中指定类别,我想实现允许您使用像 [woof taxonomies=product_cat:auto] 这样的简码的功能,它将使用特定功能自动确定当前类别,例如,这个(该功能已经过测试并且可以工作):

function show_product_category_id() {
    $cat = get_queried_object();
    $catID = $cat->term_id;
    if (empty($catID)) {
        //
        if (strpos($_GET['really_curr_tax'], 'product_cat')) {
            $catID=str_replace('-product_cat', '', $_GET['really_curr_tax']);
        }
        else {}
    }
    else {}
    echo $catID;
}

当然,我可以为这个函数创建一个简码,并将其添加到主题的functions.php

add_shortcode( 'show_product_category_id', 'show_product_category_id' );

它会起作用的。但我不能使用这样的结构:

[woof taxonomies=product_cat:[show_product_category_id]]

因为 Wordpress 中的嵌套短代码不起作用。因此,显然,我需要在 woocommerce 中添加不仅可以指定 product_cat:35,还可以指定 product_cat:auto 的功能。

我怎么能意识到它?或者,还有没有办法在 wordpress 中使用嵌套的短代码?

【问题讨论】:

    标签: php wordpress woocommerce plugins shortcode


    【解决方案1】:

    当然,你不能将一个短代码嵌套在另一个中,但是你可以做的是在另一个短代码中嵌入一个短代码,如下所示:

    function woof_current_product_category_id() {
        $term    = get_queried_object();
        $term_id = 0; // Initializing
    
        if ( isset($_GET['really_curr_tax']) && false !== strpos( $_GET['really_curr_tax'], 'product_cat' ) ) {
            $term_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
        } elseif ( is_a($term, 'WP_Term') ) {
            $term_id = (int) $term->term_id;
        }
        return do_shortcode("[woof taxonomies=product_cat:$term_id]"); // Always use return for a shortcode
    }
    add_shortcode( 'show_product_category_id', 'woof_current_product_category_id' );
    

    代码进入活动子主题(或活动主题)的functions.php文件中。

    所以用法就是:[show_product_category_id]

    【讨论】:

    • 这是我需要的,当然。我还为结果制作了 [show_result_product_category_id] 短代码(而不是 [woof_products])。感谢您的解决方案
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多