This is a bit for the techy mommies out there. Specifically the Wordpress techy mommies.
For this site, I needed a special template for the recipe category. So all posts in the Recipe Cat’ are directed to a pretty recipe template (Category-20.php). HOWEVER, since sub-categories like breakfast or desserts are not part of the parent category “recipes”, they are redirected to the regular template. Darn!
So this piece of code will take the category, and it’s babies and direct them to the special template you’ve created. See this for more instruction on creating custom templates.
Place the following code in the functions.php file. When this function is called in the category template, it will search for a parent category, and then return the ID, if it exists. If there is no parent, it will return the category’s own ID. (credit to:Sivar)
//returns a category's parent id if there is one
function get_parent_category() {
foreach ((get_the_category()) as $cat) {
if ($cat->category_parent) return $cat->category_parent;
else return $cat->cat_ID;}
}
Then, place the following code into the category.php file. The “20″ is my recipe category’s ID. Insert your custom template’s category ID. (credit to: Otto)
<?php if (get_parent_category() == 20) {
include(TEMPLATEPATH . '/category-20.php');
return;
}
?>
And the coolest thing happens: When someone looks for a recipe, they are directed to my recipe category template. And when they look for a subcategory of recipes - like main meals or beverages, then they are again directed to the recipe template! I feel as though I have mastered fine art when a code bit actually works! woo hoo!
You can contact me if you need more help with this and I’ll see what I can do - no promises!