Manually 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:

1
2
3
4
5
6
7
<?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 log in 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 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 a custom single post template based on categories? For example, posts in the 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* 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 hosting 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.

After that, you can 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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 and paste the contents of your theme’s single.php template and use it as a starting point.

More info on this link.
You could also try this one, if you’d like different approach to the same problem.

Comments

comments

Leave a Comment

Show Buttons
Hide Buttons