Skip to content

Instantly share code, notes, and snippets.

@rogierborst
Forked from GaryJones/gist:1258784
Created May 25, 2012 08:52
Show Gist options
  • Save rogierborst/2786753 to your computer and use it in GitHub Desktop.
Save rogierborst/2786753 to your computer and use it in GitHub Desktop.
Using the template_include filter in WordPress
<?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;
}
@camskene
Copy link

This will include sub-categories of sub-categories - https://gist.github.com/4643794

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