Blog Tips 1

Why you would want to create custom single post templates in WordPress

Custom post templates are best applied on specific posts that are meant to stand out from the rest of your website and wow the audience. They offer you the ability to create unique, completely custom, layouts, which can be beneficial for a wide range of websites and occasions.

For example, you can use custom post templates for posts that belong to a trending category or posts made by specific guest authors or contributors. Moreover, you can create specific post templates for each author on your website, making their content visually recognizable. You can even create templates for specific post types, especially if you’ve added any custom post types. These are only a few examples to give you an idea of the range of possibilities offered by creating WordPress single post templates.

Simply put, custom post templates allow you to create a layout that isn’t otherwise available through your theme or the use of page builder plugins. By knowing how to code them, you will gain more flexibility for customizing your website content.

Qode Themes: Top Picks

VIEW COLLECTION

Bridge New Banner

Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme

Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme

Startit

Fresh Startup Business Theme

How to create custom single post templates in WordPress

Now that you have a clearer picture of the benefits involved in making custom post templates, let’s take a look at how you can do so. We divided this part of the article into two sections. One covers the requirements you need to know before you start coding, and the other illustrates how a custom template can look using an example we created.

Understanding the template file hierarchy

As we mentioned at the start, template files are in charge of displaying various pieces of dynamic content on your website. For them to work properly, a strict Template Hierarchy structure must be observed. WordPress automatically checks whether a page or post has a custom template assigned to it before using any of the template files provided by your theme.

In the past, this was only true for pages (i.e. page templates), but the functionality was extended to posts and custom post types starting with the WordPress 4.7 update. As such, the current hierarchy for posts and custom post types is the following:

  • custom page/post template file
  • single-{post-type}-{post-slug}.php
  • single-{post-type}.php
  • single.php
  • singular.php
  • index.php

Let’s clarify what this means.

Firstly, the {post-type} notation represents the slug of a specific post type, while the {post-slug} represents the slug of a specific post item. The files with names that contain the {post-type} notation will only apply to custom post types, which are registered using the register_post_type() function. For example, if you registered a custom post type with portfolio-item as its slug, then a file within this hierarchy would be named single-portfolio-item.php.

Secondly, a custom template file is the only one that doesn’t have to follow the strict naming conventions shown above. We deliberately put page/post as part of the name to emphasize that custom template files can be written for pages, posts, and custom post types in a uniform way. This was also part of the WordPress 4.7 update that we mentioned earlier.

The reason for this flexibility of use lies in the template header, which is the first part—and a mandatory one—every custom template file. It serves to register the template and give some additional information about it. The template header is, in fact, a simple comment that specifies the template name which post type, or types, it was written for.

The template header has a strict structure; the relevant pieces of information are specified after the Template Name: and Template Post Type: strings. No other information is required, but if you choose to add some, it can be helpful for anyone reviewing the file. Also, since the template file has the .php extension, it has to begin with the opening PHP tag: <?php. However, the closing PHP tag (?>) can be placed later in the code, depending on the main content of the file.

Having explained that, you can see an example of how a custom template file could start below.

<?php
/**
* Template Name: No Sidebar Template
* Template Post Type: post, page
*/

Using this template header, you can register a custom template called No Sidebar Template and make it available for both posts and pages. That means that No Sidebar Template will appear as an option both in Page Attributes on a single page, as well as in Post Attributes on a single post.

Book an appointment

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Soak up inspiration,
expertise and Everything else

Faucibus duis tincidunt ullamcorper suscipit cras. Cursus sagittis in dapibus enim eu leo justo sed egestas, morbi mauris quam est.
Faucibus duis tincidunt ullamcorper suscipit cras. Cursus sagittis in dapibus enim eu leo justo sed egestas, morbi mauris quam est.
Faucibus duis tincidunt ullamcorper suscipit cras. Cursus sagittis in dapibus enim eu leo justo sed egestas, morbi mauris quam est.
Scroll to Top