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 1


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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.