,

如何在 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]
«
»
  • 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
  • hq720 4
    2025-01-22
  • image3 694x345 1
    2025-01-22
  • edm beta
    2025-01-22
  • edm beta
    2025-01-22
  • edm beta
    2025-01-22
  • 2025-01-22
  • 0J53CV6 0
    2025-01-22
  • 2025-01-22