I have this structure

Category > Subcategory > Post

I´m trying to display some content if the user it´s on a specific category, or in it´s subcategory or post.

I´m using this code but it only works if the user is in the category.php, not if the user it´s on a single.php that belongs to the that parent category:

    <?php if(is_category('catering')){?>
        <div class="logo">
            <img src="https://wordpress.stackexchange.com/questions/73420/<?php bloginfo("template_directory') ?>/images/canal.png" />
        </div>
        <div class="carta_catering">
                          <p>texto</p>
        </div>
    <?php    }  ?>

I tried with if(in_category) but it still didn´t show anything on single.php.

Any ideas?

2 Answers
2

The is_category Conditional Tag checks if a Category archive page is being displayed – hence it is expected to return false on single post pages, whether the post in question is in said category or not.

To check for the latter condition, make use of has_category. If you want the content to show up on category as well as single post pages (in the category), combine the two:

if ( is_category( 'catering' ) || has_category( 'catering' ) ) {
     // display something
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *