Create Page Template

How to Create a Page Template in WordPress?

In this article we will tell you about three ways to create templates for regular WordPress pages. Each method has certain advantages and disadvantages. But before we start, we would like to briefly talk about pages and how they differ from the posts.

In WordPress, you can create pages and posts. Their difference is that posts are in the feed on the main page; categories are indicated for post; posts cannot be treelike; and pages are used to such content as “About Me,” “Contacts”, “Site Map”; have no categories, have a tree structure. Posts are typically designed for the history information (by addition time), and pages for  the tree structure that is not time dependent.

Pages are similar to posts – they are located in a single database table and the data they have are almost the same: title, text, and additional fields, etc. Both are the posts, but of different types: the pages are treelike and organized by creating parent and child pages, and posts are split together by creating headings and tags. In WordPress you can create additional types of posts –  treelike or other ones.
Often, you need to create a separate page template to display information significantly different from the rest of the pages. Creating a page template in WordPress, you can completely change the page: remove the sidebar, a footer, a header, etc.

Method 1: Through File with Arbitrary Name and Connecting to Admin Panel (Classic Method)

This is probably the most common way to create a page template to WordPress. To do this, create a .php file, for example, tpl_my-page.php in the theme folder, and at the very beginning of the file write a note that the created file is a template for pages:

Template Name: My page template
<!-- Here html/php code template -->

Now at the creation of a page in the admin panel in “Page Attributes” we can select a “template”:

RELATED:  How to Download WordPress Database Using phpMyAdmin?


  • By creating a template, we can conveniently use it for different pages. For example, we can create a template without the side panel and use it on different pages.
  • You can only get posts with the specified template. For example, you can display all pages with the template “Services” (file servises.php). Sometimes it is convenient. Template file name is stored in metapol _wp_page_template, therefore, to show the page with the specified template you need to create a request for custom field.


After you create a template file in the theme folder, you need to go to the admin panel and set the template for the page. Sometimes doing it is not very comfortable. So if you meant to use a template for just one page, use the second method.

How Does it Work?

When you enter the admin panel on the edit page, WordPress scans all files of the template for the presence of the string:

Template Name: ***

The string can be located anywhere and in any way in the file.

All files with similar strings are collected and displayed in the template selection unit “Page Attributes”.
At the page publication, in any field _wp_page_template is written template or the default file name, if the template is not selected:

_wp_page_template = default _wp_page_template = tpl_my-page.php

Now, when a user visits a page, WordPress _wp_page_template check box if the template is installed, it uses a template file. Otherwise, the search continues in the template hierarchy.

Method 2: Through File With Certain Name (Template Hierarchy)

This way means the creation of a file in the folder of the theme with a certain name: page-{slug-page}.php  or  page-{ID-123}.php. At the time of creation, the page receives the slug (alternative name). It is applied in the URL of the page and can be changed.

RELATED:  What is Classic Editor in WordPress?

To create a template in this way, you need to find the slug of the page and create a file in your theme folder. Suppose our slug as in the picture is contacts, then create the  file page-contacts.php. and fill it with the desired code (you can copy the contents of the page.php template file and edit it as you wish). Now when you visit the page, you can see the new template. Similarly, you can take the ID (even 12) pages, and create a file page-12.php.


There’s no need to go into the admin panel and set the template file. The template starts working immediately after the creation of the file. It is convenient when developing.


A template is created only for one particular page. It depends on the slug of the page- if it changes, the template will not work. If you use the ID, then the dependence of the slug will be lost, but it becomes unclear in the theme file, to what page refers the template (if several templates have ID).

It’s practically useless when writing templates, much more – plugins. It can be used when  you correct your website, where slug or ID of the pages are known in advance.

Method 3: Through Filter ‘template_include” (Coding)

It is an advanced method, even more difficult, but together with the complexity it offers great opportunities. With this method, pattern for any page can be specified the page entry categories publication on any site or any group in general publications. See examples of the description:

// Filter passes the variable $ template – the path to the template file. Changing this way we change the template file.
add_filter ( 'template_include', 'my_template');
// filter function
function my_template ($ template) {
#analog of the second method
// if it is a page with a slug portfolio, use the template file page-portfolio.php
// use is_page () conditional tag
if (is_page ( 'portfolio')) {
if ($ new_template = locate_template (array ( 'page-portfolio.php')))
return $ new_template;
# template for columns group
// this example will use a file from the folder tpl_special-cats.php threads
// as a template for columns with ID 9, titled "Uncategorized" and slug "php"
if (is_category (array (9 'Uncategorized', 'php'))) {
return get_stylesheet_directory (). '/tpl_special-cats.php';
# template for records by ID
// template of the file is located in the plugin folder /my-plugin/site-template.php
global $ post;
if ($ post-> ID == 12) {
return wp_normalize_path (WP_PLUGIN_DIR). '/my-plugin/site-template.php';
# template for pages of the type "book"
// it is supposed that the file of the template book-tpl.php is in the theme folder
global $ post;
if ($ post-> post_type == 'book') {
return get_stylesheet_directory (). '/book-tpl.php';
return $ template;

This code must be placed in the file of functions.php theme or plugin, or connect in another way. As you can see in the filter example template_include conditional tags are active, global variables are set: $wp_query, $post, etc.

RELATED:  How to Schedule Empty Trash Automatically in WordPress?


  • You can set a template for any page or a group of pages. Almost complete carte blanche in operation.
  • You can create a template for writing a plugin.


The need to write a code, and connect it separately (i.e., in functions.php).

How Does it Work?

Read the description of template_include filter.

Similar Posts

Leave a Reply

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