Categories: how to assign a different template to each subcategory of the same main category?

sports

  • live score
  • positions
  • To transfer

As we saw above … the main category is Sports with three subcategories (live score, classifications and transfer) … Can each of these subcategories have a different personalized category template … that is, each subcategory with a unique template?

Here is my code below …

// Different template for subcategories
function wpd_subcategory_template ($ template) {
$ cat = get_queried_object ();
$ children = get_terms ($ cat-> taxonomy, array (
& # 39; parent & # 39; => $ cat-> term_id,
& # 39; hide_empty & # 39; => false
));

yes (! $ children) {
$ template = Locate_template (& # 39; category-sports-livescores.php & # 39;);
} elseif (0 < $cat->category_parent) {
$ template = Locate_template (& # 39; category-sports.php & # 39;);
}

returns $ template;
}
add_filter (& # 39; category_template & # 39 ;, & # 39; wpd_subcategory_template & # 39;);