Don’t use template_redirect
to load alternate templates as you can break any functionality that uses this hook to run on a lower priority than yours. The right hook is template_include
. This is according to Mark Jaquith, one of the WP lead developers. https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/
That said, it might be more appropriate to use the category_template
filter in this case.
I use this, based on a code snippet I found and badly didn’t keep a note of the source 🙁
function wpse_233263_parent_category_hierarchy() {
$templates = array();
$category = get_queried_object();
if ( $category->category_parent != 0 ) {
$parent = get_category( $category->category_parent );
if(!empty($parent)) {
$templates[] = "category-{$parent->slug}-{$category->slug}.php";
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
}
} else {
// Otherwise use the usual default category template files
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = 'category.php';
return locate_template( $templates );
}
add_filter( 'category_template', 'wpse_233263_parent_category_hierarchy' );
It fires when a category template is called for and essentially sets up one list of templates if you’re in a child category or sets up the default list if not.
In a child category you can have a template category-parent_slug-child_slug.php and if that doesn’t exist fall back to the parent. Tweak the list to suit your needs.