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?

  • 1
    Did you enable categories for pages somehow then? It's not native functionality for pages to have categories.
    – t31os
    Aug 8, 2011 at 15:15

1 Answer 1


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.

*   Add categories to Pages.
*   from http://shibashake.com/wordpress-theme/add-tags-and-categories-to-your-wordpress-page

    function add_custom_tags_box() {
        add_meta_box(   'categorydiv', __('Categories'), 'post_categories_meta_box', 
                'page', 'side', 'low'); 
       register_taxonomy_for_object_type('category', 'page');

Hope this helps.

  • Afaik you'd only have add_theme_support() for pages.
    – kaiser
    Aug 8, 2011 at 18:16
  • Ah, so simple when one realises what one is doing wrong! Thanks for your patient explanation of why what I was doing would never work ;-) - I'll experiment with that code when I have a free few minutes. Aug 14, 2011 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.