How to Create WordPress Custom Single Post Template

WordPress Custom Single Post Template

The WordPress templates are used for creating great websites. These templates are designed for catering to various different needs of the users. The use of websites is different and this different use creates different needs.

Think about a business website, a news website, a blog etc they all require different templates to fulfill the demands and thus WordPress has been proactive in responding to these demands.

There are certain situations when the website has a certain use of the template but wish to create a part of the website in a different format. For example, a website might want a different template for featured articles or story in a specified category.

This whole demand will require the use of a WordPress custom single post template and not the common template which is used by the website.

The users who have such specific demands should not worry as working with these custom templates is as simple as any other template.

WordPress has a huge collection of such custom templates which can be chosen from to pick the best one for the use. The users of WordPress can even create these single post templates with ease and do not have to go through the hassle of choosing.

How to Create WordPress Custom Single Post Template?

You will have to require the use of the certain code for doing this. The code can directly be copy-pasted and one does not have to write the lengthy code by themselves.

Open a plain text editor on the system that you are using – Notepad, word etc and paste the following code-
1)<?php
2)/*
3) * Template Name: Featured Article
4) * Template Post Type: post, page, product
5) */
6)
7) get_header(); ?>

This is a code that is used for a new template which is known as featured articles and this code allows the creation of posts, page and product types.

When you have created the code file save it as wpb-single-post.php on the system. Now you will require an FTP client and upload this file on the WordPress theme folder.

Then you will have to login to the WordPress account and go to the admin area and then you have two choices either to create or edit the post. You will now see the post attribute meta box where the drop box shows an option to select the template.

Here locate the featured articles and select that option. Now since the template is all empty when you select it you will see a white screen.

If this happens then you will have to copy the code and paste it to the file and upload it again. Now you can add features to your single post template. You can add CSS, sidebars, create a full-width template or whatever you wish to add.

Now we were talking about the need of the single custom post in the category. This is a little different process and should be done with the use of a different set of codes.

The set of codes are long but you don’t have to worry about them all you have to do is copy paste them in the functions.php file and use.

1)/*
2)* Define a constant path to our single template folder
3)*/
4)define(SINGLE_PATH, TEMPLATEPATH . ‘/single’);
5)
6)/**
7)* Filter the single_template with our custom function
8)*/
9)add_filter(‘single_template’, ‘my_single_template’);
10)
11)/**
12)* Single template function which will choose our template
13)*/
14)function my_single_template($single) {
15)global $wp_query, $post;
16)
17)/**
18)* Checks for single template by category
19)* Check by category slug and ID
20)*/
21)foreach((array)get_the_category() as $cat) :
22)
23)if(file_exists(SINGLE_PATH . ‘/single-cat-‘ . $cat->slug . ‘.php’))
24)return SINGLE_PATH . ‘/single-cat-‘ . $cat->slug . ‘.php’;
25)
26)elseif(file_exists(SINGLE_PATH . ‘/single-cat-‘ . $cat->term_id . ‘.php’))
27)return SINGLE_PATH . ‘/single-cat-‘ . $cat->term_id . ‘.php’;
28)
29)endforeach;
30)}

The codes check for the request by the WordPress and when it is made then it tells the WordPress to look for templates in the folder for the WordPress theme.

The next step is to create a template file using the code. Connect to the WordPress site using the FTP client or file manager in cPanel.

And then go to wp content> your-theme-folder. Now you must create a new folder here under the current theme folder by the name ‘single’.

Open the folder and create a new file inside it. Name this file based on the category you wish to create. One thing that you will have to remember here is that the file name should be like single-‘name’.php file.

For example, if the file contains travel tips then the name will be single-cat-travel-tips.php file. Etc. The cat is short for category and it is followed by the name of the file.

The template files will be empty and now you can add the content from the php file here. The templates are ready for edit and the users can add all the desired changes here.

The process once completed should be checked by visiting the post. The template which is created will be posted on the category chosen.

The categories will be created in alphabetical orders and the users can create as many such categories as they like following the same rules.

In the case where the category is created but the template for that category is missing then the WordPress will fall back to the default single.php template of the theme.

There are so many ways in which one can create single templates and use them. The WordPress platforms allow the users to do the thing on their own if wished. They can follow the simple codes and create a successful template.

The WordPress has always been a step ahead to help the users. If there are any doubts then the users should think about the online community support which is very strong for WordPress users. The rest of the platform is very easy to use and user-friendly.

The custom single templates posts will stand out along with the rest of the website content when they are created following the right steps.

Leave a Reply

Your email address will not be published. Required fields are marked *