one category template for multiple categories

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.

Read More

hope that makes sense and any pointers welcome.

dan.

Related posts

Leave a Reply

2 comments

  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.