Make Wordpress subcategories use Category Template

Matrym picture Matrym · Jun 25, 2010 · Viewed 11.4k times · Source

I've got a category template: category-projects.php

This category has subcategories, but they're refering to the template category.php for instructions instead of the parent category. How do I make subcategories refer to parent category templates in the cascading order of template references?

*Note, I'm talking about category level urls, not posts.

Answer

Richard M picture Richard M · Jun 25, 2010

One way to do this is to hook into the template_redirect action in your functions.php file:

function myTemplateSelect() {
    if (is_category() && !is_feed()) {
        if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) {
            load_template(TEMPLATEPATH . '/category-projects.php');
            exit;
        }
    }
}

add_action('template_redirect', 'myTemplateSelect');