Custom Single Post template WordPress

How to create Custom Single post template in WordPress

Davor 09/12/2020 Tips & Tricks

Learn how to create a custom Single post template without coding in WordPress. Most of the premium or advanced WordPress themes are doing this via a hook that is extending the default WordPress behavior.

That kind of customization can be very complicated for users without PHP coding experience, so we present you with this easier way to do it.

Table of content

  • Intro
  • Copy single post template
  • Remove sidebar markup
  • Make changes to the new single post template
  • Assign a new single post template to the posts
  • Conclusion

How to add an additional single post template?

The most common case where we have a need for an additional custom single post template is, for example, having a single post template with or without the sidebar.

This guide can be used in other cases as well. The basic concept is to have a new single post template that you can choose from the Post editor page and easily assign it to the desired pages. You can create an additional single post template or custom post type templates in pretty much the same way.

Now, let start creating, should we? 🙂

Copy single post template

So, in order to make it to work without coding, we need to create a new page template based on the single.php file located in our theme folder.

Remove sidebar markup

Make the needed changes to the template HTML structure and remove the sidebar markup from it.

This requires you to remove the PHP include that looks like this:

<?php get_sidebar(); ?>

Also, make sure that you remove the DIVs and other HTML markups for the sidebar if it’s placed in your single post template.

(*)Once you make these changes, make sure that the newly created template have all closed DIVs and that it won’t break the look of the pages.

Make changes to the new single post template

Now, go ahead and copy the single.php template and name it, for example, “single-no-sidebar.php”. Now, open this new file for editing in your favorite text or code editor.

At the top of the page, you see a comment section that contains the name of the post template. For example:

<?php
/**
 * Template name: Single
 * Template for displaying all single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package YourThemeName
 */

get_header();
?>

As you can see, it contains the markup from your single.php. It can warry in your case, but it’s not the point.

The main and most important this is the part that declared the template name.

* Template name: Single

Now, rename this template to, for example, “Template name: Single No Sidebar” and save changes.

Then we need to add the Template Post Type declaration so that this new template becomes selectable inside our single post editor in the admin area.

Add this line of code “Template Post Type: post” below the template title so that it looks like this:

<?php
/**
 * Template Name: Single No Sidebar
 * Template Post Type: post
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package YourThemeName
 */

get_header();
?>

Save changes to the file and head to the WordPress admin area.

Assign a new single post template to posts

Open up the post you want to assign this new template to, for editing.

At the sidebar of your single post editor, you will see the widget “Post Attributes”. The widget contains one option “Template” and you can select a newly create single post template “Single No Sidebar” from the dropdown menu and save changes. See this image:

select assign custom single post template e1599879332229

That’s it. Your post will use the newly create single post template.

Conclusion

This should be relatively easy even for beginners. If you are having issues with creating a custom single post template or following this guide, feel free to ask for help in the comments section below. You are also invited to share any feedback on this post as well.

Davor Veselinović

I'm Davor, founder of Worda Themes. Working as a Full Stack Developer with more than a decade of experience with WordPress. I build themes and plugins and enjoy writing useful blog posts and share my ideas with others.

Leave a Reply

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