-
-
Save rogierborst/2786753 to your computer and use it in GitHub Desktop.
Using the template_include filter in WordPress
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
<?php | |
add_filter( 'template_include', 'ja_template_include' ); | |
function ja_template_include( $template ) { | |
if ( !is_category() ) | |
return $template; | |
$category_info = get_category(get_query_var('cat')); | |
if ( $category_info->parent == 0 ) | |
return $template; | |
$parent_cat = get_category($category_info->parent); | |
// construct a possible file url | |
$subcat_url = get_stylesheet_directory() . '/subcategory-' . $parent_cat->slug . '.php'; | |
// return that url if it exists. Otherwise, return the normal template | |
return is_file($subcat_url) ? $subcat_url : $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will include sub-categories of sub-categories - https://gist.github.com/4643794