Skip to content

Instantly share code, notes, and snippets.

@stefanpejcic
Last active July 19, 2019 17:44
Show Gist options
  • Save stefanpejcic/e2c4b0d97bf0d65c41991027002d430b to your computer and use it in GitHub Desktop.
Save stefanpejcic/e2c4b0d97bf0d65c41991027002d430b to your computer and use it in GitHub Desktop.
Different single post template based on category
define(SINGLE_PATH, TEMPLATEPATH . '/single');
add_filter('single_template', 'my_single_template');
function my_single_template($single) {
global $wp_query, $post;
foreach((array)get_the_category() as $cat) :
if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
endforeach;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment