in_category() works in single.php but not in page.php?

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:

Read More
<?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?

Related posts

Leave a Reply

1 comment

  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.