Creating a Custom Single.php template

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:

  1. 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.)
  2. 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.WordPress Category ID Number
  3. Make a back up copy of single.php, call this single-original.php.
  4. 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
    $post = $wp_query->post;
    if ( in_category('3') ) {
     include(TEMPLATEPATH . '/single2.php');
    } else {
     include(TEMPLATEPATH . '/single1.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.

  5. 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).
  6. 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.
  7. Upload single.php, single1.php and single2.php as well as style.css (if you made changes to it) to your theme’s folder.
  8. Refresh your web page and click on one of the post titles in the category for which you are using single2.php.

Angela Bowman

Front-end WordPress developer since 2007 building highly custom websites for nonprofits and small businesses. Experienced in nonprofit administration, grant writing, and technical writing. Love high altitude hiking and backyard chickens.

View all posts by Angela Bowman

16 comments on “Creating a Custom Single.php template

  1. 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! 🙂

  2. 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

  3. 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.

  4. 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.

  5. 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!

  6. 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’);
    }
    ?>

  7. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *