If in parent category

I have this structure

Category > Subcategory > Post

Read More

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="<?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?

Related posts

Leave a Reply

2 comments

  1. 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
    }
    
  2. if ( in_category( 'catering' ) && is_single() ) {
    

    This will return true on single posts in the category catering but NOT the category archive page.

    if ( in_category('catering') && is_single() || is_category('catering') ) {
    

    This returns true on all single posts in the category catering and the category archive page for catering.

    if ( in_category('catering') ) {
    

    This ALSO returns true on all single posts in the category catering and the category archive page for catering.

    Couldn’t find anything in the Codex for sub categories.

    I would try the sub category i.d with the category conditional tag to see if it returns true for posts in a sub category.