0

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?

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.

  • Tell me if it works :) – jochem Dec 31 '12 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.