How to change single post template based on category. Here’s a way to alter the single post template just for posts in one specific category in a WordPress Theme and Want to figure out a way so you can target specific posts by specific categories in WordPress? Here you are.


How to change single post template in WordPress based on category

Permalinks can be altered with the different slugs for them which are

You can use these tags to customize your “Pretty” or “Almost Pretty” permalinks. A few hints:

  • Make sure to end your structure with either %post_id% or %postname% (e.g. /%year%/%monthnum%/%day%/%postname%/) so that each permalink points to an individual post.

The year of the post, four digits, for example 2004

Month of the year, for example 05

Day of the month, for example 28

Hour of the day, for example 15

Minute of the hour, for example 43

Second of the minute, for example 33

The unique ID # of the post, for example 423

A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So “This Is A Great Post!” becomes this-is-a-great-post in the URI.

A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI.

A sanitized version of the author name.

Just open your wordpress theme editor and add this filter to your functions.php and name your template file to match the category slug: single-[cat slug].php i.e Custom Single Template for the posts in Fashion category would turn out to be single-fashion.php.

If you’re using a child theme, replace TEMPLATEPATH with STYLESHEETPATH

//Gets post cat slug and looks for single-[cat slug].php and applies it
add_filter('single_template', create_function(
 'foreach( (array) get_the_category() as $cat ) {
 if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
 return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
 return $the_template;' )