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="<?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?
The
is_category
Conditional Tag checks if a Category archive page is being displayed – hence it is expected to returnfalse
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:This will return true on single posts in the category catering but NOT the category archive page.
This returns true on all single posts in the category catering and the category archive page for 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.