Created
March 3, 2016 04:22
-
-
Save nickmeagher/1265f0c5704195594a42 to your computer and use it in GitHub Desktop.
Taxonomy Term Template Children Inheritance - Children terms inherit the parent taxonomy template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this code tested? I am trying it with no results!