How to Create Custom Single Post Templates in WordPress

Do you want to create a custom single post template in WordPress? Custom single post templates allow you to easily select different templates for your posts. In this article, we will show you how to easily create custom single post templates in WordPress.

After that you can login to your WordPress admin area and create or edit a post. Scroll down a little on the post edit screen, and you will notice the new Post Attributes meta box with an option to select the template.

You will see your ‘Featured Article’ custom template listed there.

Right now your template is essentially empty so selecting it will simply display a white screen.

Let’s fix this.

The easiest way to do that is by copying the code from your theme’s single.php file and use it as a starting point.

Open the single.php file and then copy everything after the get_header() line.

Paste this code in your wpb-single-post.php file at the end. Now you can save this file and upload it back to your server.

However, this will look exactly the same as your current single post template. You can now start making changes to your custom single post template.

You can add your own custom CSS classes, remove sidebars, create a full-width template or anything you want.

Create Custom Single Post Templates Based on Category

Want to use custom single post template based on categories? For example, posts in travel category can have a different layout, than posts in photography.

This code first checks to see if WordPress is requesting a single post. If it is, then it tells WordPress to look for the template in /single/ folder of your WordPress theme.

Now you need to add template files defined by this code.

Connect to your WordPress site using an FTP client or File Manager in cPanel and go to /wp-content/themes/your-theme-folder/.

Inside your current theme folder, you need to create a new folder called ‘single’.

Now you need to open this folder and create a new file inside it. Go ahead and name this file single-cat-{category-slug}. Replace {category-slug} with your actual category slug.

For example, if you have a category called ‘News’, then you will create single-cat-news.php file. If you have a category called ‘Travel Tips’, then create a template single-cat-travel-tips.php, and so on.

Now these template files will be totally empty. As a starting point, you can copy the contents of your single.php file from your theme folder and paste them inside each of these templates.

You can now edit these templates to make your desired changes.

Once you are done, you can go to your website and view a post. It will use the template that you have created for the category where this post is filed.

Now let’s suppose you have a post filed in two categories News and Travel Tips. WordPress will automatically show the template for ‘News’ because it appears first in alphabetical order.

On the other hand, if you filed a post in a category and didn’t create a template for that category, then WordPress will fallback to the default single.php template of your theme.

Create Custom Single Post Template for Specific Authors

Let’s suppose you want posts written by a specific author to look different on your website. You can do that by using the same technique we showed for categories.

I use your method but I can’t set custom css for this posts.
for example in function.php when I check is_page_template(‘single-cat-news’) the statement not getting true and my custom css for this kind of post not running. what should I do in this case?

Following the instructions on this page (along with the small modifications noted within my comments on 12/2/17 and 12/12/17), I’ve been able to get this working reliably.

HOWEVER, I’ve run into the issue where using this method to call a custom template does not seem to get registered with the call of body_class classes.

For example, if I set a custom template on a page (where I’m using WordPress’ “Template” drop-down within the “Page Attributes” sidebar in the editing view – then the body_classes that are output would include that template name.

Such as:

<body class="page-template-my_custom_template page page-id-2..."

But, using this method, the body classes just return:

<body class="page-template-default page page-id-2..."

Any idea on how to ensure the body classes reflect a custom single post template?

I created a single-cat-news.php file for “News”. My “News” category has two child: “Camera” and “Smartphone”. The post in “Smartphone” display in single-cat-news.php (S after N in alphabet), but the post in “Camera” display in default single.php (C before N in alphabet). I want post in “Camera” display in single-cat-news.php, what must I do? I selected both “News” and “Camera” for my post.

When using this approach to specify a custom post template for a single post within a specific category, I was finding that WordPress was NOT defaulting back to single.php in instances where a single-cat-WHATEVER.php file didn’t exist for a specific category.

I fixed this by adding the following line before the end of the function:

else
return SINGLE_PATH . '/single.php';

So that, this code:

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

Thank you for your great tutorials. I am having trouble with this one. I added the code to my functions.php but I’m not getting the single post template. Do I need to make a call to the function or does WordPress do that? Is there some way I can debug the function?

I just want posts of a particular category to display differently when you link to them.

Hi! Thank you for this tutorial, but I’m quite new to web design and I started creating a whole wordpress theme. Everything works fine on my website, but I am trying to make it possible to add pages that all use a template I created, (like the page of a product in a catalogue) but I have no idea how to do that. I first tried to do so by creating a page template in which I put loops that display the content of an article or a page, such as its title, its text and the values of some custom fields. To do so I used a query_posts, but it requires to put the post’s or page’s id with “page_id=” or “p=”, which means the template is for one specific page, and I would have to create a template for each new page I add to this catalogue , and precising the id of the page/post manually in it, while what I wanted was to create a single template for all the pages of the catalogue…
So while doing my research to find a solution, I found your article, and I tried to create a post using the new single post template, but no matter which template I select in the list, when I go to the url of the post it displays my home page… I’m probably doing everything wrong, and I’m not even sure I understood what a single post is and if it’s really what I need… I would really appreciate it if you could give me some clues on what I should do to solve my problem, or if there’s some tutorial somewhere that could help me…
Thank you,

We are not sure we understand your problem completely. However, if you just want to create a template to be used by any page then custom page template is the right approach. Since you want users to be able to see any page, this means you don’t need to use query_posts, your loop will begin with the standard

I have set this up to work with categories, however when I select a post that does not have a category it fails to pick up the default template and does not display the post. I suspect my theme may be at fault. How would I hard code the single.php in the parent directory as the default to use if no category template exists ?

Works perfectly if I copy the single.php into the /single/single-cat-name.php file but I don’t want 40+ duplicate files.

This is exactly what I was looking for, thank you for spelling this out so simply! It leaves me with a question though: I removed my sidebar but now there’s just an empty space where it used to be. How can I stretch the main content section to cover the entire space? Thanks again!

That’s because your theme has defined content area width relative to the sidebar. You need to use inspect tool to find out which CSS class your conntent-area uses and then adjust by adding custom CSS to make it full width.

That’s a great article and should helps a no-coder person like me. Customization of template would be something scary that you might break the theme altogether. Do you recommend make the custom template on a child theme or is it okay to do it right on the theme. Thanks a bunch.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Get $10 off on OIO Publisher, a robust ad management plugin for WordPress.

Featured In

About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).