1

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.

1

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.

  • 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 – v3nt Mar 24 '11 at 16:44
0

ah - found it here!

Make all subcategories use the template of its category parent?

thanks...

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.