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;
}
@GaryJones
Copy link

  1. http://codex.wordpress.org/WordPress_Coding_Standards
    These are also programmatically testable using PHP_CodeSniffer and https://github.com/mrchrisadams/WordPress-Coding-Standards . My advice is that if you're creating snippets of code for others, then help save those who do have a want / requirement to follow the WP CS by writing your snippets with them. Same for documentation., which is why my fork also links back to the gist URL so they can see where it was likely copied from when they look at it in 6 months time.

If you want to improve your own gist above, then you can just Edit it (button near the top). If you want to start from my new fork (which is a fork from your code, which is a fork from my code, which is a fork from Jared), then you can just create new fork, as you did previously.

@camskene
Copy link

How could this be modified to include sub-categories of the sub-categories?

@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