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 2


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' );
  • I think this is exactly what im looking for. Where do i need to put the code?
    – TBHM admin
    Commented Nov 25, 2016 at 11:40
  • In your theme's functions.php or in a plugin.
    – Milo
    Commented Nov 25, 2016 at 11:43
  • you just need to substitute subcategory.php for the file you would like to become the parent template
    – Marco V
    Commented Jan 10, 2020 at 16:26
  • @Djanym RE your suggested edit to test isset( $cat ) first: I do generally appreciate caution, but in what circumstances can category_template get called when get_queried_object() is null?
    – Rup
    Commented Feb 12, 2022 at 15:29

I have edited your code to add more functionality. For cases where someone would want to have a different template for each child category. For example if you have categories ordered like this:

  • continent
    • country
      • city

And you need a different template for city. First we look if city has a child, if not we call the template for city. The rest of code is to check if a category has a parent.

// 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;
add_filter( 'category_template', 'wpd_subcategory_template' );

Your Answer

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

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