This tutorial shows you how to use a different template via get_template_part() for parent and child terms of a post. You could make this work for custom post types or custom taxonomies too, but in my case it was just regular posts and categories.
The site has a category called “Recipes”, and a bunch of child categories like “Beef”, “Poultry”, “Breakfast”, etc…
The recipe index needed one template, while all child terms needed to share another template.
Thankfully, after some googling I came across a really cool function called cat_is_ancestor_of().
Conditionally pull in a template
Inside my category.php file I pulled a different template based on the term. See the code below.
Then, in my child theme I included 2 files in the /includes/ directory.
- recipe-parent.php
- recipe-child.php
From there you can do whatever you’d like. The contents of my parent file is some static HTML just showing links to each child category. The child categories show recipes in 3 columns with just the title and featured image.
I hope this helps and saves you the time I spent figuring it out!
Leave a Reply