in_category() question
-
the WordPress Codex says that in_category() is used to query if a post belongs to a specific category. It doesn’t seem to allow multiple categories (i.e., you don’t seem to be able to use it to set up an if statement for categories 1,2, and 3) so I’ve been trying to find ways to work around that.
For example, I want a certain set of navigation tools to appear when posts in categories 2, 3, and 4 are being viewed, but NOT when posts in any other category are being viewed.
Right now I’m doing this:
<?php if (in_category(2)) : ?>(stuff)
<?php endif; ?>
<? php if (in_category(3)) : ?>
(stuff)
<?php endif; ?>
<? php if (in_category(4)) : ?>
(stuff)
<?php endif; ?>
(everything else on the page)
This is, needless to say, a little cumbersome. I was wondering if I could create a “master category” for categories 2, 3, 4 — for the purpose of this post, let’s call it category 5 — and use that for all of them. What I mean is, if 2, 3, and 4 are sub-categories of category 5, can I do this:
<?php if(in_category(5)) : ?>
(stuff)
<?php endif; ?>
and do the same thing as my first example?
- The topic ‘in_category() question’ is closed to new replies.