When doing WordPress theme development the first thing you will learn about is the template hierarchy, this is the order of templates WordPress will use to display the content. WordPress actually only needs one template file to work correctly, the index.php file. Depending on what content you are viewing you can create a different page template to show this in a different way. For example if you are looking at a post content then WordPress has a hierarchy of template files it will attempt to use to display the content. First it will look in the theme for the * single-{post_type}.php* file and use this to display the content. If the single-{post_type}.php isn't there then WordPress will search for the template file single.php to display the content. Finally if single.php file doesn't exist then WordPress will fallback and use the index.php file to display the content.
- single-{post_type}.php
- single.php
- index.php
Category Template Hierarchy
There is a template hierarchy for other areas of content in WordPress, in this example we are going to look at the category hierarchy. - category-{slug}.php
- category-{id}.php
- category.php
- archive.php
- index.php
This allows use to create a template file for a specific category or all categories. This means I can have a category of products and can display this content differently to the other categories by creating a theme file of category-products.php. The problem comes when you are using category hierarchies and have a sub-category to products of TVs and want to make sure that this sub-category uses the same template file as products. There is nothing in the hierarchy for category-{parent-id}.php or category-{parent-slug}.php. This means when I view the TVs category content it will not use the category-products.php file but will display the content using category.php. Meaning all the sub-categories will look exactly the same and could be completely different to the parent category, so we need to edit the template hierarchy and get WordPress to look for the parent template file.
Setup Parent Category Template File
To change the category hierarchy we need to use the filter category_template. The return of this filter will be the template files that are used by WordPress if they exist. First we need to get the information of the current category which we can do by using the function get_queried_object(), this will return the category object holding all the information about the current category. Using this category object we can add the default category template files to array category-slug.php and category-id.php. Then we check to see if the current category has a parent category, if it does then we search for the parent category by using the get_category() function, if this isn't empty then we add the extra templates to the array using the parent slug and parent ID. Finally add the default category.php template and return the array populated with the new category hierarchy.
function pu_parent_category_hierarchy()
{
$category = get_queried_object();
$templates = array();
// Add default category template files
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
if ( $category->category_parent != 0 )
{
$parent = get_category( $category->category_parent );
if(!empty($parent))
{
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
}
}
$templates[] = 'category.php';
return locate_template( $templates );
}
add_filter( 'category_template', 'pu_parent_category_hierarchy' );