在 WooCommerce 侧边栏小部件区域显示最受欢迎的产品标签

Display Most Popular Product Tags in WooCommerce sidebar widgets area

我找到了这个代码 (http://devotepress.com/faqs/display-popular-tags-wordpress) 并且我使用了短代码 ([wpb_popular_tags]) 但我没有看到任何结果。

如何使用此代码显示最受欢迎的 WooCommerce 产品标签?

这是他们的代码:

function wpb_tag_cloud() {
    $tags = get_tags();
    $args = array(
        'smallest' => 10,
        'largest' => 22,
        'unit' => 'px',
        'number' => 10,
        'format' => 'flat',
        'separator' => " ",
        'orderby' => 'count',
        'order' => 'DESC',
        'show_count' => 1,
        'echo' => false
    );

    $tag_string = wp_generate_tag_cloud( $tags, $args );

    return $tag_string;
}

// Add a shortcode so that we can use it in widgets, posts, and pages
add_shortcode('wpb_popular_tags', 'wpb_tag_cloud');

// Enable shortcode execution in text widget
add_filter ('widget_text', 'do_shortcode'); 

首先,你必须知道但你不知道的可能是:
经典 WordPress post 标签与 WooCommerce“产品标签”有很大不同,后者具有不同的自定义分类 'product_tag'

所以您不能使用 WordPress get_tags() 获取产品标签。

Instead you should replace it with get_terms( 'product_tag' ) this way:

function wpb_tag_cloud() {
    $tags = get_terms( 'product_tag' );
    $args = array(
        'smallest' => 10,
        'largest' => 22,
        'unit' => 'px',
        'number' => 10,
        'format' => 'flat',
        'separator' => " ",
        'orderby' => 'count',
        'order' => 'DESC',
        'show_count' => 1,
        'echo' => false
    );
    $tag_string = wp_generate_tag_cloud( $tags, $args );
    return $tag_string;
}

// Add a shortcode so that we can use it in widgets, posts, and pages
add_shortcode('wpb_popular_tags', 'wpb_tag_cloud');

// Enable shortcode execution in text widget
add_filter ('widget_text', 'do_shortcode'); 

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

USAGE - You will need to:

  1. Add the "text" widget in your woocommerce widget bar area.
  2. Add in the editor of this "text" widget the short code [wpb_popular_tags] (then save)

This time you will get All your "most popular" product tags *(The ones that you have set and enabled for your product)*s.

在 WooCommerce 3+ 中测试并且完美运行。