4

I'm using the Default Post Type post for various purposes. To sort them I'm using different categories. And I'm designing different category layout using the slug preference, like: category-book.php, category-notice.php etc., and everything was just fine.

But just a moment ago found that, any subcategory of the parent category 'book' is redirecting to index.php (or archive.php, or category.php) because they are not recognizing their parent's category template (category-book.php), because their slug is different.

How can I make the child categories recognize their parent's template design?
Do I have to make a custom template using a WP_Query() for that?

8

Thanks to @Rarst for guiding me to the right direction. Using his direction I googled again and again and found a blog article of WerdsWords with an excellent bit of code snippet filtered to category_template as Rarst suggested me, and the good news is: it worked for my cause:

function new_subcategory_hierarchy() { 
    $category = get_queried_object();

    $parent_id = $category->category_parent;

    $templates = array();

    if ( $parent_id == 0 ) {
        // Use default values from get_category_template()
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
        $templates[] = 'category.php';     
    } else {
        // Create replacement $templates array
        $parent = get_category( $parent_id );

        // Current first
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";

        // Parent second
        $templates[] = "category-{$parent->slug}.php";
        $templates[] = "category-{$parent->term_id}.php";
        $templates[] = 'category.php'; 
    }
    return locate_template( $templates );
}

add_filter( 'category_template', 'new_subcategory_hierarchy' );

Link to the article:

  • Why this is not a default setting I will never know! Thanks very much for this. – M_Willett Apr 23 '15 at 12:37
1

The category archive branch of template hierarchy only operates on actual category queried.

If you take a look at get_category_template() it doesn't look up hierarchy.

You can use category_template filter (named dynamically from {$type}_template further down the chain) to adjust the choice as necessary.

  • Thank you very much for guiding me to the right direction. With your suggestion what I found is mentioned as another Answer. But hesitating actually which one to accept, because your Answer was the pioneer, but that answer is with the exact pin-point solution. :( – Mayeenul Islam May 29 '14 at 5:42
  • 1
    @MayeenulIslam always accept the answer that is closest and most helpful to solving your issue :) – Rarst May 29 '14 at 7:55
0

Complementing the @Rarst sugestion, I needed the function to use the top-level parent template.

function new_subcategory_hierarchy() { 
    $category = get_queried_object();
    $parent_id = $category->category_parent;

    // Create replacement $templates array
    $templates = array();

    if ( $parent_id == 0 ) {
        // Use default values from get_category_template()
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
        $templates[] = 'category.php';     
    } else {
        // Start from the current term
        $parent = get_category($parent_id);

So I add this here:

        // Climb up the hierarchy until we reach a term with parent = '0'
        while($parent->parent != '0'){
           $term_id = $parent->parent;
           $parent  = get_term_by( 'id', $term_id, $category->taxonomy);
        }

Ending:

       // Current first
       $templates[] = "category-{$category->slug}.php";
       $templates[] = "category-{$category->term_id}.php";

       // Parent second
       $templates[] = "category-{$parent->slug}.php";
       $templates[] = "category-{$parent->term_id}.php";
       $templates[] = 'category.php'; 
  }
  return locate_template( $templates );
}                
add_filter( 'category_template', 'new_subcategory_hierarchy' ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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