All posts on your WordPress site will be formatted using the single.php template. The single.php file is used when you click on the post title on the home page of the blog, and you see just the single post. This is efficient, but what if you have a Category of your website where you want the post page to look different, for example, a photo gallery that you want to take up the full page width with no sidebars? Here’s a bit of homework for you to make a custom single.php template for a particular Category:
- Put some content on your site if you don’t have any content. (Note: You can download andimport dummy content from http://wpcandy.com/articles/easier-theme-development-with-the-sample-post-collection.html. Simply, download and unzip the XML file and go to Tools > Import > WordPress and import this file into your site.)
- Take note of the Category ID # of the category for which you want to create a custom Single.php file. To get the ID #, mouse over the Category name in the Admin area and look in the browser footer.
- Make a back up copy of single.php, call this single-original.php.
- Open single.php in your HTML editor. Delete the contents of single.php and place the following code in it. Substitute the number 3 for the Category ID # you noted in step 2 above. [php]<?php
$post = $wp_query->post;
if ( in_category(‘3’) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} else {
include(TEMPLATEPATH . ‘/single1.php’);
}
?>[/php]What this code says: If the post is in Category 3, then use the single2.php template to display the post. If the post is not in Category 3, then display the post using single1.php.
- Open single-original.php in your HTML editor and choose File > Save As and call this file single1.php. Choose File > Save As again and call this file single2.php. You should now have four files: single.php, single1.php, single2.php, and single-original.php (we won’t actually use single-original.php — this is for backup purposes only).
- Open single2.php in your HTML editor. Make whatever changes you want to make to this file to have it be unique to the category you specified above.
- Upload single.php, single1.php and single2.php as well as style.css (if you made changes to it) to your theme’s folder.
- Refresh your web page and click on one of the post titles in the category for which you are using single2.php.
Hi, thanks for sharing.
Should I register a custome single.php as custom post type?
These instructions are a bit old as most themes have better ways of doing this.
What specifically are you trying to do?
Hi Angela,
I am working with a theme that uses the Custom Post type … now I have modified the custom post admin page so that the editor is not visible, but the post title and a bunch of other custom meta boxes… now for some reason when I hit “Update” the post does not save my post title.
Do you have any suggestions on how to get the Custom Post type to save the title? Or should it do this automatically?
My rewrite option is set to True, so I’m not sure what else I’m missing.
Your help would be greatly appreciated! 🙂
Which plugin are you using to create your Custom Post Types? I really like the Types plugin http://wordpress.org/plugins/types/ as well as http://pippinsplugins.com/easy-content-types/. These really help as you don’t have to do this manually in your functions file and inadvertently mess something up.
I’m looking to show only a secondary menù for all the post of a specific category. I found the way to show only the secondary menù in the page of that category but no for the post belong at that category. Do you think your solution can work for this?
Thank you
Hey thank you WordPress Girl 🙂
I’m quite late to the party, and this is probably a dumb question too, but where do I find this single.php file to open? I’ve bene wanting to make some changes to it and I know all about that, but I can’t find where the template files of my theme are.
Thanks in advance for your help.
Hi Kim,
This depends on your theme. Some theme frameworks don’t have that file. Most do, though. You can find it in: wp-content/themes/yourthemename folder. These instructions don’t work with all themes.
Hello,
Quick question: There is a way to do this, but in a per-post basis with an attributes that later can be set from the post editor (like page attribute)?
Thanks,
No, I haven’t found a way to do that. I think you have analyze what you do need to be different and accomplish it with other tools. For example, if you need the sidebars to be different, you can use a plugin for that.
Note to other users: the form filtered out some of the code formatting so a simple copy and paste of my code above will not work. Simply compare what I have with what WP Girl wrote (the only difference is the addition of the elseif portion) and you should be fine. Happy coding!
Great article! Just wanted to add that it may be useful to add an elseif statement to the code above to allow for an even more dynamic approach to the ‘single split’ trick. Thanks again!
post;
if ( in_category(‘3’) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} elseif ( in_category(‘4’) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} else {
include(TEMPLATEPATH . ‘/single1.php’);
}
?>
Thanks, Dan! I’ll fix the code just in case it didn’t come across correctly.
Thank you for this. The WP codex confused me on this. It said to put this code in the loop (which didn’t work). Cheers to you!
I am a big fan of your blog and I read it regularly. Keep up the good work!
This was the solution I was looking for. Thank you so much!!
What if the theme I am using doesn’t have a single.php and only an index.php?
You can open the index.php file and choose File > Save As and save it as single.php.