简体   繁体   中英

Woocommerce | Need to show content in a div only if a certain product category is selected

I need to show content in a div in Woocommerce only if a certain product category is selected

I'm editing archive-product.php file and have added

<?php
if(in_category('cg-odobreno')){
?>
    <div>
        /**/
    </div>  
<?php } ?>

This in_category part is related to actual categories not product categories if I'm not mistaken. How can I select a product category?

You're looking for is_category. More @ https://developer.wordpress.org/reference/functions/is_category/

<?php if( is_category( 'cats' ) ) {
//... Best cats page template ever...
} else if( is_category( 'dogs' ) ) {
//... Dogs are better anw!...
} else if( is_category( array( 'cats', 'dogs' ) ) ) {
//... Oh boy! Cats & dogs in the same spot! things are gonna get uggly...
} else {
//... Sadly no dogs nor cats here...
}; ?>

You can use slugs, names or ids.

In_category() will use in case if you want to check that category exists in particular post/product

In your case you can do it with is_category().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM