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.

I created a website that's using Wordpress as its CMS.

There are parent pages -> sub categories -> content within each sub category.

Some of the 3rd level pages are generated using posts with categories. I would like to assign a template to a parent page so that the look of the site remains consistent according to each parent template. Additionally, is it possible to assign a template to a post category?

share|improve this question

1 Answer 1

They do not inherit the parents template.

Wordpress has some neat ways to make special templates for certain categories or custom post types (taxonomies). You can apply this to all categories, certain categories by ID or slug and archive pages. The sad part is it does not work in a hierarchical way, you have to make a page for each category..

You can read all about it and how it works on this page: http://codex.wordpress.org/Category_Templates

If you scroll to the bottom of that page you will also find a "Related" section where you can find out how it works for pages, tags, custom taxonomies, author templates and such.

Note: Also check out http://codex.wordpress.org/Template_Hierarchy

BUT there is a fix for this. You can use this code in your functions.php (or make a plugin)

add_action('template_redirect', 'inherit_cat_template');

function inherit_cat_template() {

if (is_category()) {

$catid = get_query_var('cat');

if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
include( TEMPLATEPATH . '/category-' . $catid . '.php');
exit;
}

$cat = &get_category($catid);

$parent = $cat->category_parent;

while ($parent){
$cat = &get_category($parent);
if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
exit;
}
$parent = $cat->category_parent;
}
}
}

In category page this look for 'category-XX.php' template and if it doesn't exist it looks for the parent cat's template and so on. And if no category template to the top level cat, then it goes back to the normal template hierarchy.

share|improve this answer
    
Tell me if it works :) –  jochem Dec 31 '12 at 1:33

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.