How to use different single post page in WordPress

WordPress, by default is setup to use one “single.php“ for every post. if you wanted to display a different layout or different code dependent on the specific categories a post or filed into?
To do this with WordPress; one needs to use a bit of PHP in the “single.php” file and create category specific files.
In the below example lets assume the following:

  • Category 2 is Blog
  • Category 3 is Articles section
  • Category 4 is News section

Put this code in “single.php” as follows:
<?php
$post = $wp_query->post;

if ( in_category(‘2’) ) {

include(TEMPLATEPATH . ‘/single-blog.php’); }

elseif ( in_category(‘3’) ) {

include(TEMPLATEPATH . ‘/single-articles.php’); }

elseif ( in_category(‘4’) ) {

include(TEMPLATEPATH . ‘/single-news.php’); }

else {

include(TEMPLATEPATH . ‘/single-default.php’);

}
?>
Files must be created e.g. “single-blog.php“, “single- articles.php” and “single- news.php” file referenced above. Also a “single-default.php” file should also be created so that any post that is not in any of the specified categories (2,3,4) will just use a default template file – “single-default.php“.