Posted in: WordPress Themes

Creating Custom Page & Post Templates in WordPress

Do you want to have flexibility of using multiple page design layouts using templates in WordPress?

Though latest blocks allow for easy customizations and layouts for posts and pages, sometimes the ideal solution requires a ready-made template that displays content in a specific way without creating layouts as part of the content.

Earlier, WordPress allowed adding page templates by creating files within the theme. One could choose any template for a page (and not posts). Now, WordPress (since WordPress Version 4.7) has extended the page templates to all types of content.

WordPress supports creating custom templates for pages as well as other post types such as posts and custom posts.

Create custom page templates in WordPress

To create a template that can be used globally by any page, or by multiple pages, one needs to create a custom template.

WordPress reads the contents of the theme files and identifies the available templates. For ease, some developers add a filename prefix such as page_fullwidth.php

Here’s how we create a custom template in WordPress.

Create a file in the theme directory folder and include the opening PHP comment at the top to include the template name. You should use a descriptive text as the template name so that the template can be easily selected.

<?php
/*
Template Name: Full Width Layout
*/
// Page template code goes after this.

It’s a good idea to copy the page.php (or post.php) file and rename it. You can then add the PHP comment to convert it into a template.

When creating or editing a page, you will find the template option in the right hand side under attributes.

When creating a page template, it should not be named as page-example.com or similar name with the prefix “page-“.

WordPress has reserved the page-{slug}.php file name structure for the specific page with {slug}. Meaning, if you create a page template with name as page-about.php, it will be used for the page where the permalink slug (URL slug) is “about”.

Create custom templates for posts and custom post types in WordPress

By default, the page template is only available for “Pages”. It is not used by posts or other content types.

However, WordPress allows creating templates for posts and custom content types as well. One can include the post types supported by the page template within the template opening PHP comment. To create a page template to specific post types, add a line under the template name with the post types you would like the template to support.

<?php
/*
Template Name: Full Width Layout
Template Post Type: post, page, events, testimonials, customposts
*/
// Page template code goes after this.

Though the page template has been a common knowledge, the use of page templates for posts and custom post types hasn’t picked up yet.

The nifty option to add custom templates for posts and any custom post types is a feature that can help create stunning content without customizations by end-users. A theme developer can include post and custom post type templates that makes the life easier for website bloggers and content creators.