This section will show you how to create a page template that can be used globally by any page, or by multiple pages. We’ll start by creating a two-column template file. If you’re following along and building the theme we started in Your First Theme, open wp-content/themes/my-first-theme and add a new folder called page-templates.
Some developers will group their templates with a filename prefix, such as page_two-columns.php
Important! Do not use
page-
as a prefix, as WordPress will interpret the file as a
specialized template, meant to apply to only
one page on your site.
For information on theme file-naming conventions and filenames you cannot use, see reserved theme filenames.
A quick, safe method for creating a new page template is to make a copy of page.php
and give the new file a distinct filename. That way, you start off with the HTML HTML is an acronym for Hyper Text Markup Language. It is a markup language that is used in the development of web pages and websites. structure of your other pages and you can edit the new file as needed.
Add this opening statement to the very top of the file. It states the template’s name.
<br />
<?php /* Template Name: Two Columns Template */ ?><br />
In your own projects, you can replace “Two Columns Template” with any name you like. It’s a good idea to choose a name that describes what the template does as the name is visible to WordPress users when they are editing the page. For example, in your other page templates you could call it Homepage, Blog or Portfolio.
This example from the TwentyTwelve theme creates a page template called Front Page Template:
<?php<br />
/**<br />
* Template Name: Front Page Template<br />
*<br />
* Description: A page template that provides a key component of WordPress as a CMS<br />
* by meeting the need for a carefully crafted introductory page. The front page template<br />
* in Twenty Twelve consists of a page content area for adding text, images, video —<br />
* anything you’d like — followed by front-page-only widgets in one or two columns.<br />
*<br />
* @package WordPress<br />
* @subpackage Twenty_Twelve<br />
* @since Twenty Twelve 1.0<br />
*/<br />
Add the following <p>This is a global page template for two columns </p>
under your <?php get_header(); ?>
so that when you load a page using the template you can see that it’s working. You’ll remove this at the end of your project.
Right above <?php get_footer(); ?>
code at the bottom of the file add <?php get_sidebar()?>
. When you build your sidebar later on, this template tag will pull in your sidebar and make your page have two columns. We’ll talk more in depth about these topics later on in this handbook. Be sure to save your file.
Once you upload the file to your theme’s folder (e.g., page-templates), go to the dashboard Page > Edit screen to assign your new custom page template to your Blogs Page on the right hand side under attributes you’ll see template. Select the Two Columns Template. Update the page and you’ll have successfully implemented your first global template.
The select list has a maximum width of 250px, so longer names may be cut off.
To create a template for one specific page, copy your existing page.php
file and rename it with your page’s slug or ID:
page-{slug}.php
page-{ID}.php
For example: Your About page has a slug of ‘about’ and an ID of 6. If your active theme’s folder has a file named page-about.php
or page-6.php
, then WordPress will automatically find and use that file to render the About page.
To be used, specialized page templates must be in your active theme’s folder: /wp-content/themes/my-first-theme/
Change the statement you added in the Template Heirarchy tutorial section to This is the page-about.php file
and save. Now when you visit your About page on your website WordPress will use this file to render the page. Neat!