I have categories ordered like this: continent->country->city I have managed to have different templates for continent and country(parend and child). But I need also different template for a city category(category inside first child).
1 Answer
I have found answer, based on this post
For cases when someone wants to have different template for child category. For example, if you have categories ordered like this: continent->country->city. And for example, you need a different template for city. First, we look if city have a children, if not, we call city template, the last child. In else statement we look if current category has a parent and based on that we display country template. And continent category template will be untouched, it will have category template.
// Different template for subcategories
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
$children = get_terms( $cat->taxonomy, array(
'parent' => $cat->term_id,
'hide_empty' => false
) );
if(!$children) {
$template = locate_template('category-country-city.php');
} elseif( 0 < $cat->category_parent ) {
$template = locate_template('category-country.php');
}
return $template;
}