skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search