How to Create Post Templates in WordPress?

Since WordPress 4.7, the website web application has started supporting page templates for posts. The ability to create, use and select the post templates for posts means developers can support and allow users to select different types of custom layouts for blog posts in WordPress. It is a great development towards use of multiple layouts and flexibility in displaying and presenting the content.

WordPress has been supporting custom page templates for pages (not posts) for a very long time. allowing developers to create various layouts for specific pages. This feature had been limited to the ‘page’ post type and not was not available to other post types. Now, the page template option can be used for all types of posts (including custom posts) and pages.

Here’s how to create a post template in WordPress.

A WordPress posts template defines two header elements in comments which is picked up by WordPress. In addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: post, page, custom.

Here’s an example:

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, custom
*/

Using this simple additional tag, you can now use the template for posts, pages and all other custom posts types. So, you’ll be able to select this full-width template for posts, pages, and custom (custom post type name).

When one or more templates exists for a post type, the ‘Post Attributes’ meta box is shown in the WordPress dashboard editing page for that post. The ‘Post Attributes’ label can be customized per post type using the 'attributes' label when registering a post type.

Backward Compatibility

If you have defined templates for posts in your theme, these will be shown as page templates in WordPress webbsites using versions before WP 4.7. It might not be a very big issue in terms of functionality, but WordPress versions before 4.7 will ignore the Template Post Type header and show the template as page templates, even though the new page templates might only works for regular posts or custom posts. This might confuse the users. You can avoid the problem by disabling the posts templates on old WP versions and enable backward compatibility. You can hook into the theme_page_templates filter and exclude the post templates from the list.

/**
* Hides the custom post template for pages on WordPress 4.6 and older
*
* @param array $post_templates Array of page templates. Keys are filenames, values are translated names.
* @return array Filtered array of page templates.
*/
function wpflux_exclude_post_templates( $post_templates ) {
if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
unset( $post_templates['templates/custom-post-template-1.php'] );
unset( $post_templates['templates/custom-post-template-2.php'] );
}
return $post_templates;
}

add_filter( 'theme_page_templates', 'wpflux_exclude_post_templates' );

In the code above, templates/custom-post-template-1.phptemplates/custom-post-template-1.php are the path of your post templates relative to the theme folder. The post templates will now be ignored on older versions of WP and not be shown in the page templates list. You can learn more at WP Make.

Leave a Reply

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