Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Did you enable categories for pages somehow then? It's not native functionality for pages to have categories. –  t31os Aug 8 '11 at 15:15
add comment

1 Answer

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.

share|improve this answer
    
Afaik you'd only have add_theme_support() for pages. –  kaiser Aug 8 '11 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. –  Christopher Woods Aug 14 '11 at 22:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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