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.

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.
Comments
3 Responses to “Create Custom Single Post Templates for Specific Posts or Sections in WordPress”Share Your Opinions
Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!
Please make sure that you have read our Comment Policy.










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