Learn how to fix WP – Beginner’s Guide for WordPress

Learn how to make WordPress custom single post templates

Learn how to make WordPress custom single post templates

Are you interested in developing a unique single post template for WordPress?

Learn how to make WordPress custom single post templates – When using custom single post templates, you are able to use several layouts for your individual blog articles. There are many different page layouts for WordPress themes, and you can even make your own if you need to.

We’ll demonstrate how to quickly generate unique single post templates in WordPress in this article. We’ll give you several options, so you can choose the one that works best for you.

Learn how to make WordPress custom single post templates

When Would a Custom Single Post Template Be Needed?

On occasion, you may wish to give some of the articles on your WordPress website a distinct look and feel. For prominent articles or tales in a certain category, you may wish to use a different layout.

You’ll need a WordPress custom single post template in this situation.

According to the WordPress template hierarchy, the single post template is what WordPress uses by default. Each theme comes with a single.php template that is used for all of your single articles.

There may be more templates or layout options included with certain themes.

It is quite similar to making a unique page template. When modifying a post or page in WordPress, you may use the page templates that are included with the majority of WordPress themes.

single temp page theme template example

After that, let’s look at how simple it is to make customized single post layouts in WordPress. We’ll demonstrate a variety of techniques so you can choose the one that works best for you.

  1. Using the block editor, create single-post templates.
  2. Layouts for single posts may be made using theme options.
  3. Manually writing code for bespoke single post templates.
  4. Separating single-post templates based on category
  5. Customizing single post templates for certain authors.

Using the Block Editor to Create Single Post Templates

This approach is not very flexible and doesn’t truly provide a post template. However, it is the simplest method for saving and reusing your own single post layouts.

You can store and reuse blocks using a built-in functionality in the standard WordPress block editor. The group block is one such reusable building component.

You may combine several blocks and complete post layouts into one group with the group block. Once saved, you may reuse this group block in subsequent posts.

Let’s look at how to store your customized post templates using the group block.

In WordPress, you must first create a new post. Simply add a group block to the content area after that.

single temp group add group

In order to build a single post style for your WordPress site, you can now begin adding blocks to the group block.

Any blocks, such as columns, media, text, cover pictures, and more, are available for addition.

single temp block add blocks

Take your mouse up and click the group block after you are happy with the arrangement you have established.

Simply pick the “Add to Reusable Blocks” option by clicking on the three-dot menu icon.

single temp block add to reusable

You must then give the reusable block a name before clicking the Save button. WordPress will now save all of your blocks that can be used more than once, including every block inside the group block.

On your website, you may then modify any already-written content or start from scratch. Simply click the “add new block” button on the post edit page and search for your stored block under the “Reusable” category.

single temp block reusable

When you add the block to your post, WordPress will load the full group block with all the stored settings and blocks.

Your personalized layouts may be saved using this technique. It does not, however, let you alter the way your theme treats individual posts.

Continue reading the following steps if you want more freedom.

Layouts for single posts may be made using theme options

There are options built into many popular WordPress themes that let you change how the single post template looks.

You may find these options on the post edit page if your theme supports them. Depending on the theme you are using, the selections may vary.

The Astra theme, for instance, provides customization choices while editing a single post. You may alter sidebars, and conceal headers, titles, menus, and other elements with these choices.

single temp theme astra options

If your theme has single post templates, you can find them in the post editing window’s “Template” or “Post Attributes” panel.

single temp theme

These templates are full layouts that don’t need any further settings to be utilized.

Manually writing code for bespoke single post templates.

This approach is a little more complicated since it calls for editing theme files, copying and pasting code, and maybe adding custom CSS. If you’ve never done it before, check out our guide on how to copy and paste code in WordPress.

You must first launch a simple text editor, such as Notepad, on your computer, then paste the following code inside of it:

1
2
3
4
5
6
7
<?php
/*
 * Template Name: Featured Article
 * Template Post Type: post, page, product
 */
  
 get_header();  ?>

The “Featured Article” template is defined in this code and is made accessible for the post, page, and product post kinds.

On your PC, you should save this file as wp-single-post.php. The template must then be added to your WordPress website. To do that, you need to use an FTP client to move the file to the folder for your current theme.

You may now create or update a post by logging into your WordPress admin area. You will see a new “Template” panel or “Post Attributes” meta box with a choice of templates if you scroll down a bit on the post edit screen.

single temp code

Your own “Featured Article” template will be featured there.

Your template is almost empty now, so if you choose it, you’ll just see a white screen.

Fix this, then.

The simplest approach to accomplishing it is to start by copying the code from your theme’s single.php file. You must copy anything that follows the get header() line after opening the single.php file.

Then, add this code to your wp-single-post.php file’s conclusion. This file may now be saved and uploaded to your server once again.

This will, however, have the same appearance as your present single post template. The customized single post template you created is now ready for modification.

You can get rid of sidebars, add your own custom CSS classes, make a full-width template, and so on.

Separating single-post templates based on category

Would you want to use a unique template for a single article dependent on categories? You could, for instance, use a different layout for articles in the travel category than you would for those in the photography category.

Either the SeedProd Theme Builder or code may be used to do it.

Using SeedProd, creating custom single post templates by category

We previously discussed how to change the default single post template for the SeedProd Theme Builder. But it goes farther than that. SeedProd lets you add as many custom post templates as you want. For example, you could have a different design for each post category.

Simply click the “Duplicate” option next to the existing post template to create a new single post template. The theme templates page will be updated to include a new template labeled “Single Post-Copy” at the very top.

single temp category seed prod duplicate

The default Single Post template may still be modified, just as you did with the new template. Simply use SeedProd’s visual editor to make any changes you want by clicking the template’s “Edit Design” option.

In order for SeedProd to know which posts should use this template, you must add certain criteria once you have completed designing the template. In this instance, we want it to appear when people are looking at content from a specific category.

single temp category seed prod edit conditions

A new window will open when you click the link labeled “Edit Conditions” on the template.

Here, you may name and prioritize the template. Make sure to give this template a higher priority than the default Single Post template 0, which will enable it to appear in categories that match it.

single temp category seed prod conditions page

Choose “Has Category” under “Conditions” from the second drop-down menu, and then input the category name in the final box. For example, use “photography” if you want this layout to only be used for articles in the category “photography.”

Once you’re done, click the “Save” button. From now on, all future entries in the right category will use this template.

The simplest approach to generating several custom post templates in WordPress is with this method. It doesn’t need any coding expertise and won’t unintentionally corrupt any theme files.

Using Code to Create Custom Single Post Templates by Category

For individuals who feel comfortable adding code to their theme files, there is another approach.

This code must first be added to the functions.php file of your theme or a code snippets 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;
}

The first thing this code does is examine whether WordPress is asking for a single post. If so, WordPress is instructed to search the /single/folder of your WordPress theme for the template.

The template files specified by this code must now be added.

Connect to your WordPress hosting using an FTP program or the File Manager in cPanel, then go to /wp-content/themes/your-theme-folder/.

Inside your current theme folder, you need to create a new folder called ‘single’. Next, 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 instance, you would write a single-cat-news.php file if your category was named “News”. Create a template named single-cat-travel-tips.php if your category is called “Travel Tips,” for example.

single temp category code ftp

These template files are now completely empty. You may use the contents of your single.php file from your theme folder as a starting point by copying them, then pasting them into each of these templates. Then, in order to make the modifications you want, you must alter these templates.

When you’re finished, you may read the post on your website. The template you made for the category where this post is filed will be used.

Let’s say you have an article that is divided into the categories of news and travel advice. Due to the fact that it comes first in the alphabet, WordPress will automatically display the template for “News.”

On the other hand, WordPress will fall back to the standard single.php template of your theme if you filed a post in a category but didn’t build a template for that category.

Customizing single post templates for certain authors.

Let’s say you want the articles by a certain author to appear differently on your website. You may do it using code by utilizing the method we demonstrated for categories.

This code must first be added to the functions.php file of your theme or a code snippets 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';
 
}

Finally, you should connect to your website using FTP or the cPanel 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 go ahead and create it now.

You must create a template inside this folder using the author’s username as the template name. single-author-johnsmith.php, as an example.

This template will be blank, but you may use it as a starting point by copying and pasting the contents of the single.php template from your theme.

The ability to read a post written by a certain author is now available on your website. The template you made will be applied.

We really hope that this tutorial has taught you how to design unique single-post themes in WordPress. You may also like to read our tutorial “Learn how to insert code in the WordPress header and footer

Learn how to make WordPress custom single post templates

Previous Post
Next Post