All Posts Under A Certain Category Use A Specific Template

April 23, 2010 by   Filed under Webmaster

Suppose you want to use different Single template to display individual post in certain category. You can use the in_category to check what category is the post stored in and then use different Single template. In your default single.php, enter the code below. If the post is in category 1, use single1.php, elseif in category 2, use single2.php, otherwise use single_other.php.

<?php $post = $wp_query->post;
if ( in_category(’1′) ) {
include(TEMPLATEPATH . ‘/single1.php’);
} elseif ( in_category(’2′) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} else {
include(TEMPLATEPATH . ‘/single_other.php’);
} ?>


1) You must use category ID codes that exist on your site
2) In the code example, single_other.php becomes the default sidebar.
3) To set this up, rename your theme’s file single.php to the name of the new default file – in the example that is single_other.php (use whatever name you prefer).
4) Make a new file in your theme folder called single.php – paste the code I posted in that new file. that is the only thing that should be in the new single.php.
5) Make new files single1.php, single2.php, single3.php, setting them up however you wish as custom single templates.
Give them whatever file name you want. Just change the name of the file for that category in the IF statement I posted to match the actual filename you give the file.

Note: This tutorial is based on the popular StudioPress Themes and while most StudioPress/Revolution themes use the same division names in their code, not all do. Also, if you are using this for a non-StudioPress Theme (tsk, tsk) your division names will be different from the code used in this tutorial.

Readiris 12 - Discount 20%