Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

Create Custom Single Post Templates for Specific Posts or Sections in WordPress

Last updated on by
Follow WPBeginner on YouTube
Create Custom Single Post Templates for Specific Posts or Sections in WordPress

In one of our previous tutorials we shared how you can create a Custom Page in WordPress where we created a custom page template. But did you know that you can do that for single posts also? In this tutorial we will share how you can create a custom single post template for each specific post, specific author, or a specific category.

We recommend you have a basic knowledge of HTML and CSS with an understanding of how WordPress themes work before you try these tutorials.

Custom Single Post Template for Different Posts

This technique will let you create custom single post templates and you can select which template each post need to use as you are writing it. To accomplish this, you must download a plugin called Single Post Template and follow the directions below.

Then you will need to create a custom file which for the examples sake we will name customsinglepost.php. You can copy your normal single.php info and change the things that you want. Or you can create a completely different design if you so desire. But one thing you must make sure that you do is to add the following code at the top of the file that you create:

<?php
/*
Single Post Template: [Descriptive Template Name]
Description: This part is optional, but helpful for describing the Post Template
*/
?>

Once you are done creating the custom template for single posts, save it among your other theme files in your theme directory. Considering you already have your plugin activated when you go in your WordPress admin panel to write a new post, you will see a drop down box that gives you an option to select which template you want for that specific post.

Single Post Template

Note: If you do not select any template, it automatically shows the default template. You can have as many templates as you like.

An Alternative plugin that does the same job is called Custom Post Template.

This method above is ideal for specific posts, but if you want to have a completely different template for specific category, then the method above can be automated with a few codes in your file. This will save you time from editing each post and selecting the specific template especially if you have a lot of posts already published in that category. The automated method was shared by Justin Tadlock, one of the very famous WordPress theme developer.

Custom Single Post Templates for Specific Category

To use a separate single post template for each category, you will need to add the following function in your functions.php:

/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');

/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;

/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :

if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';

elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';

endforeach;

This code will tell the script to look for templates such as single-cat-uncategorized.php or single-cat-1.php within the /single folder. You must make sure that you save the files within the single folder that you will need to create in your theme directory.

Custom Single Post Template for Specific Author

Simply place this code in you functions.php and it will tell WordPress to look for templates such as single-author-admin.php or single-author-1.php and if it does find it in your /single folder.

/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . '/single');

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');

/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;

/**
* Checks for single template by author
* Check by user nicename and ID
*/
$curauth = get_userdata($wp_query->post->post_author);

if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php';

elseif(file_exists(SINGLE_PATH . '/single-author-' . $curauth->ID . '.php'))
return SINGLE_PATH . '/single-author-' . $curauth->ID . '.php';

We can’t really specify how you must create your custom single post template because that is upto you and depends on what customization you want to make, but if you get stuck feel free to ask us any question. What we must emphasize is that you must save all these custom templates in a folder named /single that must be located in your themes directory.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Mustafa Faris

    Thank you,
    This is a great tutorial.
    It work perfectly for me

  • Reto

    Hi
    How can i add manualy, just a single post template, that i created to a specific single post-type.
    I mean as you do in this explenation “Custom Single Post Templates for Specific Category”, just not automated.
    I dont want the choice of the “custom post template” plugin,
    I just want have a certain template for a certain posttype.

    Thaks for answers

  • Himanshu Goel

    Sir Thanks !
    You sorted out my half the tension via this. As i was having a theme with multiple color schemes and without generating much codes i can assign the css file to new templates.
    But the question is: What will happen if single post is posted under multiple categories i.e. 2 or more. will it show an error or will it work anonymously with any category or with the last slug or id detected.
    you can post here and mail me (in case i couldn’t find this page again)
    I really need to design my site with multiple color schemes as there are different parent categories to differentiate with others.
    Also, what will happen with sub-categories…

    • http://www.wpbeginner.com/ WPBeginner Support

      it will pickup the first category.

  • John Shine

    you should add the flowing code at the end of the code so return default template file if there is not a custom template.
    else return TEMPLATEPATH . ‘/single.php’;

  • Jeffrey

    Hi Samuel!
    Some great insight here! :) Hoping you can help me bring it to the next level…b/c I’m stumped on something. Here’s the scenario:
    -I have a feed of posts that when clicked from this feed, they open in an iframe by adding ?TB_iframe=true&width=1000&height=550″ class=”thickbox” to the link
    -Using your example of “Custom Single Post Templates for Specific Category” I am able to point them to a specific template I’ve created … all good
    -The issue is when a user encounters a post that utilizes that same category … when clicked it ALSO uses that new single-cat-temp.php :( And looks all wonky.

    Is there a way to isolate the function further to only kick in the use of the custom single when IS category && Coming from a specific page or in the iFrame?

  • Samuel

    In both code blocks, there the last part that is missing (missing end brace):

    return $single;
    }

    It is mentionned at the end of Justin’s article that you mentioned.

  • Luit Krol

    I use the plugin Custom Post Templates to select a custom made template for my custom posts. How can I make this specific template the default for this custom post type without having to select it manually?

    thanks for your help

    Luit

    • http://www.wpbeginner.com Editorial Staff

      You can save that template as single-$posttype.php, and it will be set as the default template for that custom post type.

  • mark shirley

    Neither plugin works they strip the styling out of twenty eleven child theme. The first one doesnt even give you the pull down admin selector

    • http://www.wpbeginner.com Editorial Staff

      We are using the bundled version of the first plugin in Genesis, and it seems to work just fine on our site. The plugin itself should NOT strip out your styling. All this is doing is giving you the ability to pick a different single template. If you did not code the single template properly, you will see styles being stripped out.

      • Chris

        Hi there,

        The plugin didn’t work with the latest wp 3.4.1,
        do you know how to fix it?

        The template is not showing when I create post.

        • http://www.wpbeginner.com Editorial Staff

          Did you try both Custom Post Template and Single Post Template?? At least one of them should work. We are using Single Post Template’s variation that comes bundled with the Genesis Theme framework.

  • guest

    This plugin does not work with 3.3.1 unfortunately

  • CapitalH_Press

    I used this code in my file that I created and named it single-portfolio.com

    <?php/*Single Post Template: [Descriptive Template Name]Description: This part is optional, but helpful for describing the Post Template*/?>

    Then without adding the plugin or setting any function my portfolio custom posts are using this file as their single file template, I think in WP 3.3 we no longer need plugins like that.

    • CapitalH_Press

      The code I pasted is not showing in the comment, I meant the code described in the article it self to specify the name of the file we created the commented out peace of code

  • frankandjen

    Is there anyway to get this to work with custom post type templates?

    Thanks!

  • marktastic

    My apologies if this is a simplistic question, but I just want to make a new category for posts and want all of that category to have the same custom template.

    I create the custom.php and throw it in the theme file as shown. I activate the plug in.

    Do I have to add all of the above listed code if all I care about is that one specific category? Or is there a shortcut?

    Thanks for your article!!

  • http://RottenElf.com Rotten Elf

    WOW!!!!!!!!!!! I just came up with an idea that required this, and in looking in the wp-admin panel, I noticed that custom post templates weren’t available, but I thought “wait…I can’t be the first person to need this, I’ll google it” lo and behold, you have the solution. Thank you WPBeginner (and everyone else in the WP community that shares information and techniques). I seriously needed this little tut on creating custom post templates!

    This opens up so many doors for me…..so many doors.

  • http://www.zouchmagazine.com jeff campagna

    Hey! Great Article!

    What if the element you want to change is not in the single.php file but in the loop.php file? The loop calls the post thumbnail to appear before the post title. But I want, for a specific category, to remove that thumbnail from the loop…

    Hopefully I am making myself clear.
    Thanks so much for this!

    • http://www.wpbeginner.com Editorial Staff

      If it is just for one category, then you can always create a category-slug.php file which will override your current settings :)

  • Peter

    Hi there,

    I was just wondering if it’s possible to use the template for the latest post on the front page, where I only show one post.

    • http://www.wpbeginner.com Editorial Staff

      If you just want to show one post on the homepage, utilize the custom homepage article that we have shared in our site.

      http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-homepage-in-wordpress/

      Run a wp_query for the loop to show the first post only, and style it the way you want.

      • Peter

        Thanks for the quick answer :)

        What I meant was, when I use a different post template for all my blog posts, how do I make sure that when only showing one blog post on the home page, that blog post will use, say, single-custom.php, instead of index.php or home.php?

        I could just paste the content of single-custom.php in index.php or home.php, however, I would have to do that every time I create a new blog post.

        • Peter

          To elaborate, I use a custom header for each blog post, e.g.

          <?php
          get_header('name_of_custom_header_file');
          ?>

          In my index.php file I just have

          <?php
          	get_header();
          ?>

          Also, I put in some extra elements before and after the loop.

          http://jasonsantamaria.com does what I need — shows only the latest post with a custom layout. Just can’t figure out how to do it.

        • http://www.wpbeginner.com Editorial Staff

          You might have to store the template in custom-field and then pull it that way. You can also utilize the new Post Formats to display it that way. Maybe the method shared in this article is not the best method…

          Custom Taxonomy may also be another solution…

  • http://chiefscommand.com Travis

    Also,

    In “Custom Single Post Template for Specific Author” you may want to copy the original single.php to the single template path then add:

    elseif(file_exists(SINGLE_PATH . ‘/single.php’))
    return SINGLE_PATH . ‘/single.php’;

    at the end of the code so if there is not a template set up for an author, it will fallback to a template, rather than the homepage.

  • http://chiefscommand.com Travis

    Hi,

    I am trying to use the “Single Post Template” plugin and the “Custom Single Post Template for Specific Author” method described above.

    How can I make WordPress check if there is a “Single Post Template” before checking for an Author template?

    The special single post templates I have set up need to be the first priority.

    Thanks

  • http://enedimensiones.com Javier

    Hi there, thanks for the post. Just one thing, you are missing the
    “return $single;” statement at the end of your code.

    I now it may seem that it is not needed but I will recommend use it as it may do behave randomly if not. (I got in troubles myself using the “one room” template becouse I got no comments section in the default single.php when not using “return $single.php;”)

    Cheers for sahring!!

    javier.

  • http://www.badcat.com kel

    The Art Direction plugin takes a slightly different approach. http://wordpress.org/extend/plugins/art-direction/ Unfortunately it hasn’t seen much updating lately.

  • http://item-9.com Jason Pelker

    Regarding the specific category template…

    Is there a way to use a default file (i.e. single.php) for when there is no single-cat-1.php file?

    If there isn’t an appropriate single-cat-1.php file (or whichever category is being called), the functions.php code above then uses the index.php file to call the loop.