The WordPress pages are the most used post type in big part of every WordPress project, since they are the basic and helpful. What if you need to have different type of pages? on only one with sidebar or without it? Every time you click from the fronted on a page WordPress automatically refers to page.php , if is not present then will go to index.php. If we will need to create different page layout we will need to create page template.

There are two ways to create it, by using page’s ID or its slug.
Naming you page page-17.php automatically WordPress will assign this page to the page that have ID = 17, also naming page-contact.php for the page named contact tell the WordPress to use this page template for the page named Contact. Template page with slugs have more priority than page with id, so page-contact.php will override page-17.php. From my point of view depending from the project be careful to not overpass the page template number.

How to create a page template

Creating a page template is quite simple. Need to add a piece of code at the top of the file

/**
* Template Name: Full Width Page
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
**/

After this you need to go to the specific page and specify the page template name needed to use. Best practice if you have more than 1 page template you can create a folder into your theme named “template” and put all the page template into it, WordPress will recognise them, if a page template is active you need to reset it if you change the directory of the page template.

This kind of method can be used also into posts, single-contact.php but in a short and limited way.

Using page templates allow you to create variety of page layout and structures in a easy and simple way.
Hope will be useful on your future projects.

Regards

Leave a Reply

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