Sometimes you need sub-categories to inherit their parent’s archive/category template. But it’s not supported by default. Here is a little filter that does that for you!
function new_subcategory_hierarchy() {
$category = get_queried_object();
$temp = $category;
do {
$parent_id = $temp->category_parent;
$temp = get_category( $parent_id );
} while ( $temp->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', 'new_subcategory_hierarchy' );

Share
Tweet
Email
Leave a Reply