Skip to content

Instantly share code, notes, and snippets.

@abrudtkuhl
Created September 25, 2015 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abrudtkuhl/8c66c59791f42ad45423 to your computer and use it in GitHub Desktop.
Save abrudtkuhl/8c66c59791f42ad45423 to your computer and use it in GitHub Desktop.
WordPress Single Page Template For Categories
<?php
// CATEGORY SINGLE TEMPLATES :: single-{category_slug}.php
add_filter( 'single_template',
create_function( '$t', 'foreach( (array) get_the_category() as $cat ) {
if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php";
} return $t;' ) );
// Example
// single-news.php
@syntaxc
Copy link

syntaxc commented Apr 5, 2017

Hi Andy,

Thank you for the code.
It's almost exactly what I was looking for.

A note: using TEMPLATEPATH won't work if using a child template.
This will look in the parent template directory for the single-{cat->cslug}.php file.
I replaced it with dirname(FILE) to fix this.

Thanks again,
C

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