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 have category-events.php to list my posts tagged 'events' but it wont work for 'events' child categories.

ie i have some posts tagged 'events' and one of events' children 'exhibition'. I thought it would use category-events.php automatically but it doesn't. Is this possible as i want to user category-events.php for several of 'events' child categories.

hope that makes sense and any pointers welcome.

dan.

share|improve this question

2 Answers 2

First paste this code in your theme's functions.php

function post_is_in_descendant_category( $cats, $_post = null ){
    foreach ( (array) $cats as $cat ) {
        // get_term_children() accepts integer ID only
        $descendants = get_term_children( (int) $cat, 'category');
        if ( $descendants && in_category( $descendants, $_post ) )
            return true;
    }
    return false;
}

then on your category.php or archive.php (depends on your theme) at the very top add

<?php if (post_is_in_descendant_category(33)){
 include (TEMPLATEPATH . '/category-events.php'); 
 exit;
} ?>

and change 33 to the ID of the 'events' category.

What it does is check if the current category is a child category of events and if so it uses the right theme file.

share|improve this answer
    
was using the above fine until i realised it wasn't filtering the category's sub content. It always loads the parent categories posts. ie /events/ loads events but /events/exhibitions/ also just loads /events/. How can we get it to load the parent category template but the sub category's –  daniel Crabbe Mar 24 '11 at 16:44

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.