ericrasch
10/3/2011 - 9:37 AM

Using the template_include filter in WordPress

Using the template_include filter in WordPress

<?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;

}