Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Category Template Hierarchy 'descendant-of-category' templates, allow any descendant to inherit the template of a set of parent categories.
add_filter('cth_category_template', 'descendant_templates');
* My plugin will call your function, and pass it the list of all templates
* before sending them off to find which ones match.
function descendant_templates($templates){
* The $parents variable is key, this is what I am not able to know
* from within the plugin.
* Im assuming you may have more than one top-level like collections.
* The order given here will determine the hierarchical order, should
* A particular category be a descendant of more than one of the $parents.
$parents = array ('collections', 'other-top-level-slug');
//This will be populated with template names of matched categories
$desc_templates = array(
'slugs' => array(),
'ids' => array()
foreach($parents as $parent_slug){
//This function is in the Template Category Hierarchy plugin.
if(is_child_of_category($parent_slug, null, false)){
$parent = get_category_by_slug($parent_slug);
//Collecting slugs and ID's seperately so we can insert them in the right places later
$desc_templates['slugs'][] = "descendant-of-category-{$parent->slug}.php";
$desc_templates['ids'][] = "descendant-of-category-{$parent->id}.php";
* Adding the appropriate elements to the templates array.
* The position '2' for slugs can be assumbed because if
* the current category is a descendant of any category, then
* it will already have the is-child-*.php templates in the
* list.
* @uses array_slice();
* @link
array_splice($templates, 0, 0, $desc_templates['slugs']);
array_splice($templates, 2, 0, $desc_templates['ids']);
return $templates;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment