显示WooCommerce侧栏小部件区域中的热门产品标签
问题描述:
我发现此代码(http://devotepress.com/faqs/display-popular-tags-wordpress),并使用了短代码([wpb_popular_tags]),但没有看到任何结果。显示WooCommerce侧栏小部件区域中的热门产品标签
如何使用此代码显示最受欢迎的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的后标签比WooCommerce“产品标签”非常不同的具有一个不同的定制分类标准'product_tag'
。
所以你不能使用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文件或也以任何插件文件。
用法 - 你将需要:
- 添加 “文本” 窗口小部件在您的woocommerce部件的酒吧区。
- 添加在这个“文”的编辑器部件的短代码
[wpb_popular_tags]
(然后保存)这个时候你会得到所有你的“最流行”的产品标签 *(那些您已经设置并启用了您的产品)* s。
在WooCommerce 3+测试和完美的作品。
+1
非常感谢 – arz
试试这个:http://www.wpbeginner.com/plugins/how-to-display-most-popular-tags-in-wordpress/ –