In this tutorial i will show you how to create a single post template for a whole category. Required for this tutorial is basic HTML and CSS knowledge.
To get started, past the following code in you’re theme’s functions.php.
/**
* Create a path to the single template folders
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by category
* Check by category slug and ID
*/
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';
else
return 'single.php';
endforeach;
}
What this code does is it searches for files named single-cat-yourcategoryname.php and then uses this file as a template for the single posts in that category. This code can also easily be done for one author. Instead use paste the following code in you’re functions.php
/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');
/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
/**
* Checks for single template by author
* Check by user nicename and ID
*/
$curauth = get_userdata($wp_query->post->post_author);
if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php';
elseif(file_exists(SINGLE_PATH . '/single-author-' . $curauth->ID . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->ID . '.php';
}
This code works the same as the ones for a category, just save you’re author single post template in the folder /single and give it a name like single-author-yourename.php.

Rare vraag misschien.. maar waarom zou je een template willen van een singlepost in een category?
Zodat de opmaak altijd het zelfde is of zo?
Ikzelf let eigenlijk daar nooit zo op.
Verder wel goed uitgelegd!
Dit is vooral handig als je bijvoorbeeld een categorie hebt waar je dingen uit je portfolio wil laten zien en die net een iets andere layout moet hebben dan een gewone blog post! Zoals je op deze site ziet bij portfolio. Deze hebben ook een andere layout dan deze blogpost.
Bedankt voor je comment! Zal volgende keer nog een verhaaltje erbij zetten met het nut ervan in de post zelf.