Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiemoya/2992406 to your computer and use it in GitHub Desktop.
Save eddiemoya/2992406 to your computer and use it in GitHub Desktop.
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){
if(is_category()){
/**
* 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 http://php.net/manual/en/function.array-splice.php
*/
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