Creating Multiple Single Posts for Different Categories

If each post only belongs to one category, and each category has different post template, just modify single.php, we can use the following code:

if ( in_category('fruit') ) {
    include 'single-fruit.php';
} elseif ( in_category('vegetables') ) {
    include 'single-vegetables.php';
} else {
    // Continue with normal Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // ...
}

We also can use the following:


$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single9.php');
elseif ( in_category('12') ) {
include(TEMPLATEPATH . '/single12.php');
elseif ( in_category('42') {
include(TEMPLATEPATH . '/single42.php');
} else {
include(TEMPLATEPATH . '/single1.php');
} }

If each post may bebelongs to more than one category, and the post only use 1 main template, say a post belongs to category 2, category 4, category 5, and we only choose category 2 as main category, we can not use the above method, we can use the following code:

// get category id of the current post
$cats=get_the_category();   

$cats=get_the_category();   
foreach($cats as $cat) {
	$newcatsarr [] = $cat->cat_ID; 
}

if (in_array("2", $newcatsarr))
{
    include 'single-book.php';
}
 
else
{
    include 'single-video.php';
}

Here, category book’s id is 2, and category video’s id is 1.