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.
  • Pingback: Tweets that mention Creating a Custom Single.php template | WordPress Girl | WordPress Theme, Plugin, and Styling Advicee | Boulder, Colorado -- Topsy.com

  • http://fifthandspring.com Joseph

    What if the theme I am using doesn’t have a single.php and only an index.php?

  • admin

    You can open the index.php file and choose File > Save As and save it as single.php.

  • Andrea

    This was the solution I was looking for. Thank you so much!!

  • Neil Conry

    I am a big fan of your blog and I read it regularly. Keep up the good work!

  • DWL

    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!

  • http://www.gestaltcreations.com dan

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

  • http://www.gestaltcreations.com dan

    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!

  • Angela

    Thanks, Dan! I’ll fix the code just in case it didn’t come across correctly.