Lindy Norris
Member
Posted 11 months ago #
I have two unique Categories on a site "Guest Services" and "Local Guide" that each have several child categories. I want to style these categories and their respective children different form every other category on the site. I've created two category-ID.php files of category-4.php and category-7.php.
How do I apply these unique templates to the children of the respective Categories?
Lindy Norris
Member
Posted 11 months ago #
Okay so I wasn't able to do it using category-id.php but I was able to do it with category-slug.php and adding a function.
I am curious if there is a better way to do this.
function load_cat_parent_template()
{
global $wp_query;
if (!$wp_query->is_category)
return true; // saves a bit of nesting
// get current category object
$cat = $wp_query->get_queried_object();
// trace back the parent hierarchy and locate a template
while ($cat && !is_wp_error($cat)) {
$template = TEMPLATEPATH . "/category-{$cat->slug}.php";
if (file_exists($template)) {
load_template($template);
exit;
}
$cat = $cat->parent ? get_category($cat->parent) : false;
}
}
add_action('template_redirect', 'load_cat_parent_template');
Thanks to this article: http://stackoverflow.com/questions/3119961/make-all-wordpress-categories-use-their-parent-category-template