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.

(Satisfying minimum text requirement)

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Here is a code I used to do it:

// make category use parent category template
function load_cat_parent_template($template) {

    $cat_ID = absint( get_query_var('cat') );
    $category = get_category( $cat_ID );

    $templates = array();

    if ( !is_wp_error($category) )
        $templates[] = "category-{$category->slug}.php";

    $templates[] = "category-$cat_ID.php";

    // trace back the parent hierarchy and locate a template
    if ( !is_wp_error($category) ) {
        $category = $category->parent ? get_category($category->parent) : '';

        if( !empty($category) ) {
            if ( !is_wp_error($category) )
                $templates[] = "category-{$category->slug}.php";

            $templates[] = "category-{$category->term_id}.php";
        }
    }

    $templates[] = "category.php";
    $template = locate_template($templates);

    return $template;
}
add_action('category_template', 'load_cat_parent_template');
share|improve this answer
    
Was able to use take this idea and implement a tweaked version on our site, thanks! –  Adam Nov 30 '10 at 14:29
    
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
    
Use this solution below, it works exactly right, because if there's a category-{slug}.php page for child, it uses that, otherwise uses category-{slug}.php of parent category. –  Anriëtte Myburgh May 31 '12 at 10:17
add comment

daniel Crabbe had the same issue I had. Found this site that has some code that will look for and use a parent category's template file whilst showing the appropriate posts.

I've updated it to support slugs in the file name (not just the category ID).

// Use a parent category slug if it exists
function child_force_category_template($template) {
    $cat = get_query_var('cat');
    $category = get_category($cat);

    if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->slug . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->slug . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
    } else {
        // Get Parent Slug
        $cat_parent = get_category($category->category_parent);

        if ( file_exists(TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php') ) {
            $cat_template = TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php';
        } else {
            $cat_template = $template;
        }

    }

    return $cat_template;
}
add_action('category_template', 'child_force_category_template');
share|improve this answer
    
This worked absolutely beautiful! Thanks Eric! –  Anriëtte Myburgh May 31 '12 at 10:16
add comment

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.