If you want to display single post page using different single post template, then wordpress in_category() function will help to create different single post template.
First thing you have to create different single post template like
single_portfolio.php single_news.php single_wordpress.php single_default.php //For default single post template for post other than above categories.
Above three files i create because i want to use different single post page template for my portfolio,news and wordpress category respectively.
You can simply create copy of your single.php file and rename it with new template and do the changes that you want for each category.
After that open your single.php file remove all code and add following code
<?php
$post = $wp_query->post;
if (in_category('portfolio')) {
include(TEMPLATEPATH.'/single_portfolio.php');
} elseif (in_category('news')) {
include(TEMPLATEPATH.'/single_news.php');
} elseif(in_category('wordpress')) {
include(TEMPLATEPATH.'/single_wordpress.php');
}
else{
include(TEMPLATEPATH.'/single_default.php');
}
?>




brilliant!
Perfect! Great solution! Thanks for sharing.
This is an excellent tutorial otherwise but am looking for a different variant of it. I am looking for a template which we can use to enter input of a new blog based on a template selected from the ‘Single Post Template’ drop down. The selected template will include input to custom fields as well. I would appreciate if anybody around can help me out.
TIA
Yes it can be done using the custom field.
I tried, but it’s not working… Definitely I’m missing something… Kindly anyone help me ?
let me know what problem you are having
What a brilliant solution, which beg the question, would it work if you use category ID instead? So we can format the single php template like the category-xx.php > e.g.: single-xxx.php?
Yes there is no any specific reason for the .php files. i just used names to differentiate it.. you can use what ever names to the php files.. important thing is the in_category(‘category_namge’)
you have to pass that category name.. and include respective php file.
This is exactly what I was looking for. Now my ‘songs’ and ‘blogroll’ category have different templates. Thanks!
Short and sweet. Just what I needed. Thanks!
perfect, thanks!
wow..thanks
finally I found it here..
Very simple
very functional
Grazie mille
That is BRILLIANT!!!! Thank you so much for your help that has made my life as a web designer much easier! That is exactly what I need in my web design!!!
I really like the posting … was easy to follow and very helpful while I was reading it … except, it’s not working for me.
This is driving me nuts because it seems so simple but I’m having trouble.
Because I’m using a child template, I had to change my code to:
post;
if (in_category(‘doggonecrazy’)) {
include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-doggonecrazy.php‘);
} elseif (in_category(‘humanrants’)) {
include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-humanrants.php‘);
} elseif(in_category(‘quote’)) {
include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-quote.php‘);
}
else{
include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-rainbow.php‘);
}
?>
This is actually getting the right template file to open because I’m getting
Fatal error: Call to undefined function get_header() in /home/stacey/public_html/wp-content/themes/Nala-theme/single-cat-humanrants.php on line 10
Why is it telling me that get_header() is undefined? I’ve been messing with it for hours and maybe it is obvious but I guess I’m missing the forest for the trees but I can’t seem to figure it out.
If I replace single.php with it’s orginal code, including the call to get_header() it will go back to the way it was before.
Any help would be apprectiated,
Thanks
so simple.. this is what i really need.. thanks a bunch dude…
I was messing around with filters and complicated stuff when I stumbled upon your post. Thank you.
Just what I was looking for. Thanks.
That’s what I’m searching for. Great!
Oh My God!!!!
Realy realy thank you for your share, i have been more than 72 hours without sleep for stupid this code, i founded many articles like this but all off sucks explanation and always following error, YOU ARE GOOD SIMPLE EXPLANATION for newbies like me,
Good share sir!! God Bless you!!!
Great solution. I modified it slightly to fit my needs, as I have subcategories and I wanted to include them in my template (without having to update the code each time I added a new subcategory).
Here’s my code:
post;
if (in_category( ‘cat1′ ) || post_is_in_descendant_category( get_term_by(‘slug’,'cat1′,’category’) )) {
include(TEMPLATEPATH.’/single_cat1.php’);
} elseif (in_category( ‘cat2′ ) || post_is_in_descendant_category( get_term_by(‘slug’,'cat2′,’category’) )) {
include(TEMPLATEPATH.’/single_cat2.php’);
} else {
include(TEMPLATEPATH.’/single_default.php’);
}
?>
I’ll try this once more. I forgot to include my code tags:
Thank you. Thankyouthankyouthankyou.