/Page Templates & Post Templates
page templates

Page Templates & Post Templates

What is a WordPress Page Template?

A page template is created to change the way a specific kind of page is built and how it looks. For example: if you have a cooking blog, you might have an about page which would consist of a title and some text. However, you might want to have a page that shows all the recipes that have potatoes in them. In order to do that you would have to code the page to do that by pulling in all those recipes, and that would need to be done on a separate file.

Another very popular example is a full width template. Instead of creating a page with a sidebar, you might want a full width page.

So when creating the page in the admin, you would see a drop down menu allowing you to choose from all the templates that exist in your theme.

It would look something like this:

post template

Post Template & CPT Template

As you probably noticed, in the image above it says “post template” and not “page template”. That’s because since version 4.7 templates are available for all post types. We’ll get to that in a moment.

How to Create a Page Template?

First of all you need to create a new file within your theme and call it – page-templatename.php

The Code:

At the top of your page add this:

<?php
/**
* Template Name: Full Width
*/

Now if you save your file and go create a new page and open the drop down menu, you will see your newly created template. Now all you need to do is code it to do what you want. In general, you would probably want to copy your page.php file into the newly created file and start from there.

Post Type Templates

As mentioned above, as of version 4.7 you can create custom templates for any type of post type, no matter if it’s a regular post or a post type you created specifically for your site. Unlike regular page templates, here you can reuse the same template on various post types within the same file. So just name you file something that will make sense when you look at it later and add the following to your new file:

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

 

Here we have 2 lines:

The first is the same as the page template, this is the name that will appear in the menu drop down. The second row tells us which post types can use this template, so just decide which post types can use the template and add a comma between them.

Create a Custom Template for a Specific Page

Assuming you don’t want to create a template that can be reused, you want this template to be used on a specific page and that’s it, this is what you need to do:

Copy your page.php file, create a new file and rename it like this:

  1. page-{slug}.php
  2. page-{ID}.php

So if your slug is named aboutyou name it: page-about.php.

If your page ID is 255, you name the file: page-255.php.

Join Thousands of Others and Never Miss a Post