WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've found this simple yet elegant solution for make Wordpress using the same template category on every child and grandchild categories http://stackoverflow.com/a/3117202/391929

Full code:

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');

add_action('template_redirect', 'myTemplateSelect');

But I still have one problem: in my case the root category (on the above example the "projects" category) should not use the that template, but a different one, so I made this simple change in the snippet:

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

But for some reason this is not working, can anyone see why?

share|improve this question
up vote 0 down vote accepted

I managed to solve this in a procedural way:

1 added in theme function this snippet:

add_action('template_redirect', 'load_category_tree_template');

function load_category_tree_template() {
    if (is_category() && !is_feed()) {
        // replace 'your-base-category-slug' with the slug of root category
        $base_tree_cat_id = get_cat_id('your-base-category-slug'); 
            // get current category id
        $catid = get_query_var('cat'); 

        if (is_category($base_tree_cat_id) || cat_is_ancestor_of($base_tree_cat_id, $catid)) {
            load_template(STYLESHEETPATH . '/category-your-base-category-slug.php');

Then in category-your-base-category-slug.php file i include a different other file based on current category slug:

<?php get_header(); ?>

if (is_category( )) {
  $cat = get_query_var('cat');
  $currentcat = get_category ($cat);
  $currentslug = $currentcat->slug;
// if we are in base category we know it by the slug
if ($currentslug == 'your-base-category-slug'){
        // if we are load a file with the loop for the root category
        // otherwise we are not in the root, so show the loop for all other category in the tree

<?php get_footer(); ?>

I know it can be done better, but it's quite simple solution in the end.

share|improve this answer

Your Answer


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.