Use template for posts with a particular category grandparent

I have this category structure for my blog posts:

Grandparent Category-> Parent Category -> Category 1, Category 2, Category 3

the post is assigned only to one of the grandchildren category and I’d like to use a specific template for all the categories that has that specific grandparent (I don’t want to assign them to the grandparent category because i don’t want the grandparent to appear in the post list).

function get_custom_cat_template($single_template) {
 global $post;
if ( in_category( 'Grandparent Category' )) {
      $single_template = dirname(__FILE__) . '/single-template.php';
 }
 return $single_template;
}

add_filter( "single_template", "get_custom_cat_template" ) ;

I realized that if the post is not assigned also to the grandparent category the function in_category() doesn’t work, but I’d like to have something that says –

If that post category has that grandparent use this specific template.

Thanks in advance for your help!

Read more here: Use template for posts with a particular category grandparent

Leave a Reply

Your email address will not be published. Required fields are marked *