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

Nice thought process.

Couple of things:

  1. The $subcat_url variable is mis-named - it's not a URL, but a path to a file.

  2. I'd also add a conditional in that final return of is_readable( $subcat_url ), since you're currently only checking that the string looks like a path to a file, not that it resolves to a file that exists AND is readable.

  3. Some WP code standards and documentation :-)

https://gist.github.com/2787095

@rogierborst
Copy link
Author

Gary, thanks for the comments.

  1. You're absolutely right.
  2. I'm not sure why I used is_file( $subcat_url ), I'd usually check with file_exists(), but is_readable() sounds like a good option, too.
  3. WP code standards? Didn't know there were any, and not sure if I could be bothered tbh. Documentation on the other hand, yeah, should definitely add that.

So, I'm completely new to Gists. Should I fork the improvements? Can I do commits like in Git?

@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