Guide to Create Custom Page Templates in WordPress

guide-to-create-custom-page-templates-in-wordpress-codexworld

In WordPress, page templates are a specific type of template file that are used for a specific page or group of pages. A page template only applies to pages, not for other content types. The page templates are used to change the look and feel of the pages. If you want to make a page different from other pages, create a custom page template.

This tutorial shows you how to create custom page templates that can be assigned to the pages from administrative panel. Custom page template allows you to make the different layout for different pages.

Template Hierarchy of Page Templates in WordPress

When the browser requests a page, WordPress selects which template will use to render this page. As per the WordPress Template Hierarchy, page template files are looked like the following order.

  • Page Template – WordPress looks for a custom template assigned to the respective page. If found uses it otherwise move to the next hierarchy.
  • page-{slug}.php – If a custom template is not found, WordPress looks for a specific template named with the page’s slug.
  • page-{id}.php – If a specific template with the page’s slug is not found, WordPress looks for a specific template named with the page’s ID.
  • page.php – If a specific template named with page’s slug or ID is not found, WordPress looks for the theme’s default page template.
  • index.php – If no specific page templates are assigned, the theme’s index file is used to render pages.

Creating Custom Page Template for Global Use

When you want to use a custom template globally for multiple pages, then you can be assigned the custom page template to any pages from page add and edit screen. Follow the simple two steps mentioned below to create a custom page template for global use.

Create Template File:
Create a PHP file (page-template-full.php) and placed it in active theme directory (/wp-content/themes/theme_name/). It will a best practice to group all the template file with a common filename prefix (page-). In this file, write an opening PHP comment to specify the template name.

<?php
/**
 * Template Name: Custom Page Template
 */

Rest of the code will be written after this comment block.

Assign Template to Page:
Go to the Page » Add or Page » Edit screen in your admin dashboard. On the right-hand side, you’ll see the custom template is available in the Template drop-down menu under the Page Attributes section. By selecting the custom template, it can be assigned to the respective page.

wordpress-custom-page-templates-tutorial-template-dropdown-codexworld

Creating Custom Page Template for Specific Page

The specific custom page template is used for a particular page. To create a specific page template, go to the active theme directory (/wp-content/themes/theme_name/) => copy the existing page.php file and rename it with the specific page slug or ID.

  • page-{slug}.php
  • page-{ID}.php

For example, you want to customize your About Us page with a custom page template. So, custom About Us page template file name would be page-about-us.php or page-10.php. WordPress will automatically render that file when About Us page is requested by the browser.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up