2017-08-19 64 views
1

我發現此代碼(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'); 
+0

試試這個:http://www.wpbeginner.com/plugins/how-to-display-most-popular-tags-in-wordpress/ –

回答

0

首先,你有什麼要知道,你不知道的可能是:
經典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文件或也以任何插件文件。

用法 - 你將需要:

  1. 添加 「文本」 窗口小部件在您的woocommerce部件的酒吧區。
  2. 添加在這個「文」的編輯器部件的短代碼[wpb_popular_tags](然後保存)

這個時候你會得到所有你的「最流行」的產品標籤 *(那些您已經設置並啓用了您的產品)* s。

在WooCommerce 3+測試和完美的作品。

+1

非常感謝 – arz

相關問題