I would like to have a different template for categories and subcategories
The categories template is set in categories.php
is it somehow possible to load the subcategories template from subcategories.php or something like that?
2 Answers
The template hierarchy has filters for all types of templates. Here we can use category_template
, check if the current category has a parent, and load the subcategory.php
file in that case:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if ( isset( $cat ) && $cat->category_parent ) {
$template = locate_template( 'subcategory.php' );
}
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );