Go Back

Another snippet of glory.

So, when redesigning this blog, I wanted to make a Category named “Clients”, I also wanted to make this category have a particular Custom Post Template, which basically means that whenever I set a post to have the “Clients” category, I wanted to display a different layout.

(In case you want to use this for yourself, place it on your functions.php file).

add_filter('single_template', create_function(
	'$the_template',
	'foreach( (array) get_the_category() as $cat ) {
		if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
		return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
	return $the_template;' )
);

The snippet in particular just checks your /theme-root folder for a .php filed named single-{slug}.php, so, whenever I make a new category and I want to have a custom post template for said category I just need to create a file with single-CategoryName.php and the script will do the rest.

This script was taken from here, and edited due to the single_post_template filter being deprecated.

Have a nice day.