Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:23:16+00:00 2026-05-15T14:23:16+00:00

I want to change the default template hierarchy behavior, and force all subcategory level

  • 0

I want to change the default template hierarchy behavior, and force all subcategory level pages that don’t have their own category template file to refer to their parent category template file. In my other post, Richard M. gave an excellent answer that solved the problem for an individual subcategory. Does anyone know how to abstract it?

function myTemplateSelect()
{
    if (is_category()) {
        if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) {
            load_template(TEMPLATEPATH . '/category-projects.php');
            exit;
        }
    }
}

add_action('template_redirect', 'myTemplateSelect');

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:23:17+00:00Added an answer on May 15, 2026 at 2:23 pm
    /**
     * Iterate up current category hierarchy until a template is found.
     * 
     * @link http://stackoverflow.com/a/3120150/247223
     */ 
    function so_3119961_load_cat_parent_template( $template ) {
        if ( basename( $template ) === 'category.php' ) { // No custom template for this specific term, let's find it's parent
            $term = get_queried_object();
    
            while ( $term->parent ) {
                $term = get_category( $term->parent );
    
                if ( ! $term || is_wp_error( $term ) )
                    break; // No valid parent
    
                if ( $_template = locate_template( "category-{$term->slug}.php" ) ) {
                    // Found ya! Let's override $template and get outta here
                    $template = $_template;
                    break;
                }
            }
        }
    
        return $template;
    }
    
    add_filter( 'category_template', 'so_3119961_load_cat_parent_template' );
    

    This loops up the parent hierarchy until an immediate template is found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a template for dreamweaver, and don't want to change the scripts for
I want to change the behavior of a JavaScript used to display a banner,
I have a text file where I want to change only the first line
I want to change the default timestamp of the magic columns of rails (created_at,
If all tables I want to delete from have the column gamer_id can i
I've made a change to what I thought was the main default template page
I have a simple button. I want to switch it's template(or style ...)when it
Is it possible to change the default template of each ASP.NET server control? In
I have restyled the DataGridRowGroupHeader on my datagrid because I want to change the
Is it possible to change the default cursor in Windows Mobile 5? I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.