Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.