-
-
Save GaryJones/1258784 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' ); | |
/** | |
* Apply a template to all subcategories of a certain parent category. | |
* | |
* @author Jared Atchison | |
* @link http://www.jaredatchison.com/2011/10/02/taking-advantage-of-the-template_include-filter/ | |
* | |
* @param string $template Existing path to template file | |
* @return string Potentially amended path to template file | |
*/ | |
function ja_template_include( $template ) { | |
// Parent category (News) ID | |
$my_parent_category_id = 7; | |
if ( ! is_category() ) | |
return $template; | |
// Get category information | |
$category_info = get_category( get_query_var( 'cat' ) ); | |
if ( $category_info->parent == $my_parent_category_id ) | |
return get_stylesheet_directory_uri() . '/subcategory-news.php'; | |
return $template; | |
} |
I've forked to better handle the hierarchical order... see https://gist.github.com/sardbaba/5013763
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just created a fork that's more dynamic. In this version you hardcode the template file ('/subcategory-news.php') into the function.
You could also check if there's a file called 'subcategory-' + the parent category slug + .php in the theme folder and use that one (like I do in my fork).