I’m using in_category(array)
inside The Loop to detect whether a post is categorised into the Discussion category (or has one of a couple of slugs). This works beautifully on single.php
but doesn’t even seem to be evaluated when used inside page.php
?
FWIW, here’s my code:
<?php if ( in_category( array( 'Discussions', 'discussion', 'discussion-article' ) )) :?>
<p id="discussions-prepended"><em>This is an article from our Discussions series - read more in the <a href="/discussions">Discussions section</a>.</em></p>
<?php endif;?>
On the borked page.php instance, I’ve also tried
<?php elseif:?>
<p>This post does not meet the category criteria.</p>
<?php endif;?>
but that doesn’t show either leading me to believe the code’s not being evaluated. This code snippet was, in both instances, inserted directly before <?php the_content(''); ?>
(the_content()
in single.php); it just doesn’t eval or show up in page.php. Intentional behaviour or am I doing it wrong?
Unless you have added the functionality with a plugin or custom code, Pages are not placed in Categories. Pages are organized with Parent Pages and Children Pages. This is why (I suspect) in_category() does not work in page.php
The following code in functions.php will add categories to Pages.
Hope this helps.