扩展 WooCommerce 产品过滤器插件简码

Extend WooCommerce Product Filters plugin shortocodes

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

但是,并不总是可以在短代码本身中指定类别,我想实现允许您使用像 [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 中使用嵌套简码吗?

当然,您不能将简码一个一个地嵌套在另一个简码中,但您可以将一个简码嵌入另一个简码中,如下所示:

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]