Skip to content

Instantly share code, notes, and snippets.

@nickmeagher
Created March 3, 2016 04:22
Show Gist options
  • Save nickmeagher/1265f0c5704195594a42 to your computer and use it in GitHub Desktop.
Save nickmeagher/1265f0c5704195594a42 to your computer and use it in GitHub Desktop.
Taxonomy Term Template Children Inheritance - Children terms inherit the parent taxonomy template.
/**
* Filter the taxonomy hierarchy to inject a parent level of templates.
*
* @param string $template The current template.
* @return string Filtered taxonomy template.
*/
function new_tax_hierarchy( $template ) {
$term = get_queried_object();
// If not an object, or the object doesn't have a taxonomy, bail.
if ( ! is_object( $term ) || ! isset( $term->taxonomy ) )
return $template;
$taxonomy = $term->taxonomy;
// If the taxonomy isn't hierarchical, bail.
if ( ! is_taxonomy_hierarchical( $taxonomy ) )
return $template;
$templates = array();
$parent_id = $term->parent;
if ( 0 == $parent_id ) {
// Use default values from get_taxonomy_template().
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
$templates[] = 'taxonomy.php';
} else {
$parent = get_term( $parent_id, $taxonomy );
// Current templates.
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
// Parent templates.
$templates[] = "taxonomy-$taxonomy-{$parent->slug}.php";
$templates[] = "taxonomy-$taxonomy-{$parent->term_id}.php";
$templates[] = 'taxonomy.php';
}
return locate_template( $templates );
}
add_filter( 'taxonomy_template', 'new_tax_hierarchy' );
@alibasheer
Copy link

is this code tested? I am trying it with no results!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment