【问题标题】:Display Most Popular Product Tags in WooCommerce sidebar widgets area在 WooCommerce 侧边栏小部件区域中显示最受欢迎的产品标签
【发布时间】:2018-01-27 23:24:33
【问题描述】:

我找到了这个代码 (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'); 

【问题讨论】:

标签: php wordpress woocommerce shortcode custom-taxonomy


【解决方案1】:

首先,你必须知道但你不知道的可能是:
经典的 WordPress 帖子标签与具有不同自定义分类法 'product_tag' 的 WooCommerce“产品标签”非常不同。

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

相反,您应该将其替换为get_terms( 'product_tag' )

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 文件或任何插件文件中。

使用方法 - 您需要:

  1. 在您的 woocommerce 小部件栏区域中添加“文本”小部件。
  2. 在此“文本”小部件的编辑器中添加短代码 [wpb_popular_tags] (然后保存)

这次您将获得所有“最受欢迎”的产品标签 *(您为产品设置和启用的标签)*s。

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

【讨论】:

  • 非常感谢
猜你喜欢
  • 2019-10-22
  • 2019-09-04
  • 2017-06-09
  • 2018-11-12
  • 2021-02-20
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多