sub-categories template

Make WordPress subcategories to use parent category template

subcategories templateWell today I’m going to bring you new snippet. For example, let’s say you are using the posts for various purposes like questions, pools, articles and reviews. To sort it out you will need different categories. Of-course now you need to design different category layout. This is done using the slug preference, like: category-questions.php, category-pools.php, category-reviews.php in your template folder etc.

The whole idea is great and it will work, but things goes wrong when you add subcategories to any of parent (main) categories like on our example questions, pools, reviews… To understand it we need to know WordPress hierarchy

The category/archive template hierarchy in WordPress.

category-{slug}.php
category-{id}.php
category.php
archive.php
index.php

To understand to this “issue” even that is not, we have to look at default get_category_template() function witch is in WordPress since 1.5 update.

Default WordPress GET category template function

[php]
function get_category_template() {
$category = get_queried_object();

$templates = array();

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

return get_query_template( ‘category’, $templates );
}
[/php]

If we watch closer to the get_category_template() function we can’t miss that it doesn’t look up hierarchy at all so the new function should do exactly that.

What this means? If there is a category named reviews, and the ID 13, then category archive will first look for the file category-reviews.php , then for the file category-13.php. If those php files are missing it will search for category.php. For example it also doesn’t exist, it will continue to archive.php and if there are lack of archive.php goes for the index.php to display the category content.

With this logic it’s obvious that the subcategory of category reviews, actually hosting (http://website.com/category/reviews/hosting) won’t load category-reviews.php.

So this snippet is telling WordPress to recognize if there are parent’s category template (category-reviews.php) available to use it as child’s template too.

Make subcategories to use parent category template

[php]
/****** Make subcategories to use parent category template ******/
/**
* Snippet Name: Make WordPress subcategories to use parent category template
* Snippet URL: http://wperror.com/make-subcategories-to-use-parent-category-template.html
*/
function wpe_subcategory_template() {
$category = get_queried_object();

$parent_id = $category->category_parent;

$templates = array();

if ( $parent_id == 0 ) {
// Use default values from get_category_template()
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
$templates[] = ‘category.php';
} else {
// Create replacement $templates array
$parent = get_category( $parent_id );

// Current first
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";

// Parent second
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = ‘category.php';
}
return locate_template( $templates );
}

add_filter( ‘category_template’, ‘wpe_subcategory_template’ );
[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>