A couple of times in the last several years, I’ve needed sub-categories to inherit their parent’s archive template, but it’s just not something the Template Hierarchy supports. I’ve seen several plugins that tried and failed to do it, so finally I wrote a little filter that in my testing, works any number of levels deep, from sub-sub-categories to sub-sub-sub-categories. Enjoy!
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' );
Thank you for this!
Wow wow wow. Thanks so much. It saved me.
Just what I was looking for! Thanks.
That’s a great tip! Thank you for sharing!
Thanks. It works perfectly. I also have another similar problem, how do force the sub page to use its parent page template?
Thanks for this! Worked perfectly. What plugin are you using for you social commenting?
Glad it worked for you. I’m using the comments module in JetPack.
Do you also know how to get a category to use the body class of it’s parent category?
If you’re using
post_class()
orbody_class()
, I’d just write a filter callback to add the parent class and then do add_filter() with ‘body_class
‘ or ‘post_class
‘ as the hook, and call the callback insidenew_subcategory_hierarchy()
or whatever.Tried this but it didn’t work.
function body_class_add_parent_category( $classes ) {
if ( is_category() ) {
$category = get_the_category();
$parent = $category[0]->category_parent;
$class = get_cat_name($parent);
} return $classes;
}
add_filter( ‘body_class’, ‘body_class_add_parent_category’ );
Not quite sure what’s wrong here. Any suggestions are appreciated.
First off, remember that
$classes
is an array. So you’d want to assign the returned value ofget_cat_name( $parent )
with something likeNote that
$class
became$classes[]
. Also, I added anisset()
ternary to avoid a notice.Thanks again for your help. Just learning php. This looks good but for some reason it’s not adding the parent category body class. I used the script below for single posts. This worked perfectly but I was not able to adapt it for child categories. Any thoughts?
Thanks dude!!! This is butter.
This is genius!
Thanks
Thanks for the snippet. Really useful. How would you modify the code to use it with custom taxonomy instead of default categories?
Hi Petr,
Something like the following should work for hierarchical taxonomies (untested):
Thanks for your reply, really appreciate your help.
I have tried your function, but it still doesn’t work. Here is what I am trying to achieve:
Render all products with producttype (kitchen, bathroom) taxonomy using this template:
taxonomy-producttype.php (works fine)
Render all products in kitchen category using this template:
taxonomy-producttype-kitchen.php (works fine)
Render all products in kitchen subcategories using this template:
taxonomy-producttype-kitchen.php (but it currently renders as taxonomy-producttype.php)
Any thoughts?
Hey Drew, great article, I am trying this for taxonomies, it didn’t work as Petr.
I tried debugging it, it is returning the correct template (the parent) at this line “return locate_template( $templates );”
but it didn’t work.
I used this filter “template_include” intead of “taxonomy_template”
and I rearranged the templates according to my need:
// Parent templates.
$templates[] = “taxonomy-$taxonomy-{$parent->slug}.php”;
$templates[] = “taxonomy-$taxonomy-{$parent->term_id}.php”;
$templates[] = ‘taxonomy.php’;
// Current templates.
$templates[] = “taxonomy-$taxonomy-{$term->slug}.php”;
$templates[] = “taxonomy-$taxonomy.php”;
and it worked out 😀
Thanks Drew! It worked for me. I tried several other code snippets from other sites as well, but they didn’t work. But this one worked at first go! 🙂
You save me mate! Thanks!!!!!!!!!!
Thank you ! It’s perfect. 🙂
Unfortunately, it doesn’t take sub sub category as you said. Then i make this :
instead of :
Have a nice day.
Thank you! This alteration works perfectly. As you said, the original code doesn’t work for sub-sub- categories.
Thank you for this, saved my day!
Thank you MissHD
your code works for me 🙂
thanks a lot for this great tutorial……….
I tried using this code to show a single post for a particular category name- products but it does not work for me. Somehow it picks the single.php file and not the category-products.php
I am new to php functions kindly help me achieve different templates for different category & its sub-category should also show the layout of its parent’s category template
Excellent one. Saved me from perishing… 🙂
Thanks a lot.
Shared on WPSE: http://wordpress.stackexchange.com/a/145886/22728
Great! Glad it worked for you.
Great. Works. Thank you!
I love you.
Thanks Drew! I tried a few other snippets before I found yours, and none of them worked quite right. This is perfect!
This works perfectly, thank you.
This didn’t work for me copying and pasting the snippet from your main post into my function.php file in a 2015 child theme (which I’m just starting to customise so there’s not really anything in function.php for it to clash with.
I’ve created a template for my parent category – category-artwork.php (which works) and I’d like the child categories (portraits, petportraits etc) to use this, rather than having to duplicate this for each child category, or changing the default archive page for all categories. So it sounds like your code should work but the display still seems to be the default twentyfifteen one.
Is it meant to go somewhere else? 🙂
Nevermind, I’d forgotten that when I set up the categories I initially had a category level in between the two so it wasn’t the direct parent. Have fixed that and its working fine, thanks for the code!
Brill-i-ant. Thank you! Mega helpful.
Wow, thanks so much for this!
Nice! But have question, have hierarchy Parent – Child – Child. For first two works, but for latest child – not.
That’s great. It was an immediate fox to the problem I was facing. Pasted that code into functions.php and boom.
Hi!
I have a question: If I need apply this to only one parent category and it’s child? The rest of the others parent categories must keep as usual.
Thanks
Perfecto, thank you!
Thanks!
Thank you, thank you and again thank you, this saved me tons of hours
Thank you for sharing this. Cheers!
Just what i looking fo, thanks!
Gold! Thanks a bunch for this!