,

如何在 WooCommerce 中獲取當前產品類別名稱 ?

woocommerce current product cat

如果你想在個別商品頁面讀取奇商品類別分類 , 首先前往編輯佈景主題 , 編輯 function.php 檔案並新增以下代碼

add_shortcode('techmusea_productcategory', 'get_current_product_category');

function get_current_product_category(){

        global $post;

       $terms = get_the_terms( $post->ID, 'product_cat' );
        $nterms = get_the_terms( $post->ID, 'product_tag'  );
        foreach ($terms  as $term  ) {                    
            $product_cat_id = $term->term_id;              
            $product_cat_name = $term->name;            
            break;
        }

       echo $product_cat_name;
}

新增後即代表您可以使用 shortcode 短代碼並可以新增到個別商品內容

 [techmusea_productcategory]
Oval@3x 2

免費獲取應用程式推薦列表,讓你節省超過 90% 費用

分享超過十個熱門應用程式, 包括 Youtube Premium 及Google one 省錢攻略

閱讀我們私隱權條款


«
»
訂閱追蹤討論
獲得
guest

0 Comments
Inline Feedbacks
View all comments
相關文章 ↓
  • 2023-09-04
  • monitor screen with openai logo on black background
    2023-08-18
  • 1662321011 image
    2022-09-05
  • cloudwaysytcover
    2022-10-14
  • 1658350087 namecheap Logo Large
    2022-08-10
  • silver laptop computer beside white smartphone on brown wooden table
    2022-06-27
  • 1655293663 image
    2022-06-15
  • 1654715777 screenshot 1
    2022-06-09