admin管理员组

文章数量:1322203

I'm trying to add text to the product short description by category.

So for example, for all Cake Toppers, I want to add to the bottom of the short description to show "This is a cake topper". For all products which are not cake toppers, I want to show the text "Not a cake topper".

This is the code I'm using:

add_action( 'woocommerce_single_product_summary', 'product_short_description_by_category', 20 );

function product_short_description_by_category() {
if( is_product_category('cake-toppers') ){
   echo '<p>A cake topper</p>';
    }else{ 
    echo '<p>NOT CAKE TOPPER</p>';
    }
}

But all products, including cake topper, show "THIS IS NOT A CAKE TOPPER".

What am I doing wrong here? "cake-toppers" is the slug for the category name.

Site at: /

Any help is greatly appreciated!

I'm trying to add text to the product short description by category.

So for example, for all Cake Toppers, I want to add to the bottom of the short description to show "This is a cake topper". For all products which are not cake toppers, I want to show the text "Not a cake topper".

This is the code I'm using:

add_action( 'woocommerce_single_product_summary', 'product_short_description_by_category', 20 );

function product_short_description_by_category() {
if( is_product_category('cake-toppers') ){
   echo '<p>A cake topper</p>';
    }else{ 
    echo '<p>NOT CAKE TOPPER</p>';
    }
}

But all products, including cake topper, show "THIS IS NOT A CAKE TOPPER".

What am I doing wrong here? "cake-toppers" is the slug for the category name.

Site at: http://wendyw11.sg-host/product/two-wild-cake-topper/

Any help is greatly appreciated!

Share Improve this question edited Sep 20, 2020 at 8:17 Antti Koskinen 6,0088 gold badges15 silver badges26 bronze badges asked Sep 20, 2020 at 7:26 WendyWendy 32 bronze badges 1
  • Related: wordpress.stackexchange/questions/333424/… – Jesse Nickles Commented Feb 2, 2023 at 8:58
Add a comment  | 

1 Answer 1

Reset to default 0

is_product_category() – Check If Current Page is a Product Category

Try this

add_action( 'woocommerce_single_product_summary', 'product_short_description_by_category', 20 );

function product_short_description_by_category() { 

        if( has_term( 'cake-toppers', 'product_cat' )){ 

                echo 'A cake topper'; 
        } else{ 
                echo 'NOT CAKE TOPPER'; 
        } 
}

本文标签: phpWooCommerce isproductcategory() not working