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.

How to create custom single post template in WordPress

Note: This tutorial requires you to edit WordPress theme files. If you haven’t done this before, then check out our tutorial on how to copy and paste code in WordPress.

When Do You Need a Custom Single Post Template?

Sometimes you may want a different look and feel for certain posts on your website. For example, you may want to use a different template for featured articles or stories in a particular category.

This is where you will need to create a custom single post template in WordPress.

It is very much like creating a custom page template. You would be able to select your template from the post edit screen.

Having said, let’s take a look at how to easily create custom single post templates in WordPress.

Creating Custom Single Post Templates in WordPress

First you need to open a plain text editor on your computer like Notepad and paste the following code inside it:

<?php
/*
 * Template Name: Featured Article
 * Template Post Type: post, page, product
 */
 
 get_header();  ?>

This code defines a new template called Featured Article and makes it available for post, page, and product post types.

You can save this file as wpb-single-post.php on your desktop.

Next, you need to upload it to your current WordPress theme folder using an FTP client.

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.

Select your custom single post 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.

Here is how you can do that.

First you need to add this code to your theme’s functions.php file or a site-specific plugin.

/*
* 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 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.

Creating single post template for categories

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.

First you will need to add this code to your theme’s functions.php file or a site-specific plugin.

/**
* 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_author_template');

/**
* Single template function which will choose our template
*/
function my_single_author_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';

}

Next you need to connect to your website using FTP or File Manager in cPanel and then go to /wp-content/themes/your-theme-folder/.

If you haven’t already created a folder called /single/ inside it, then let’s go ahead and create it now.

Inside this folder, you need to create a template using the author’s username in the template name. For example, single-author-johnsmith.php.

This template will be empty, so you can copy paste the contents of your theme’s single.php template and use it as a starting point.

You can now visit your website to view a post created by the specific author. It will now use the template you created.

That’s all for now.

We hope this article helped you learn how to create custom single post templates in WordPress. You may also want to see our list of extremely useful tricks for the WordPress functions.php file.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Create Custom Single Post Templates in WordPress appeared first on WPBeginner.