It’s time for the third installment in our WordPress customization series, which cover the different aspects of tweaking your WordPress themes to your liking. We previously wrote about making small CSS changes without altering a theme, and afterwards we dove into creating child themes so your modifications would be safe from updates. In this article we’ll cover creating custom page temaples, which you can use to further customize the look of your single theme pages.
What Are Page Templates?
As you may know, WordPress comes right out the box with two different types of post: posts and pages. While posts are entries commonly associated with a timeline and appear on the WordPress RSS feed, pages serve as a more static counterpart, and are often used to display more permanent content such as an about page, or a sitemap. There are actually a few additional post types that are less known– such as attachments or custom menu elements– but they are fairly secondary.
While you can create any number of additional post types with the aid of plugins, pages have one particularly defining feature: page templates. Simply put, a page template allows you to use a different appearance for any given page, making use of a different file when outputting the HTML markup. These templates come in the form of individual files, and are normally included with WordPress themes or certain design-related plugins such as eCommerce. You can then use a specific page template by selecting it from a list while editing a page in the WordPress admin.
Page templates are tremendously useful, and widely used for many different purposes. The most common use for them is to add dynamic content to a page, in such a way that it greatly reduces the amount of maintenance required in your site. For example, our premium WordPress themes at CPOThemes include page templates for a blog and a portfolio; when used, these templates will automatically output a list of posts right after the page’s content, like a list of the latest posts or a grid-like list of your portfolio items. Using these page templates has two primary advantages: first off, it gives you the ability to place your blog or portfolio virtually anywhere on your site, instead of forcing you to a particular URL. Second, it lets you add content along with these dynamic lists, so you can have greater freedom over how these pages are presented.
Creating Custom Page Templates In WordPress
Strictly speaking, when WordPress outputs a page’s design, it will make use of the page.php file located in your active theme’s directory. A page template overrides this, telling WordPress to use its own, custom layout instead. Like page.php, a page template simply consists on a single file located within your theme files. Creating your own page templates is actually a very easy task, although it may require a bit of knowledge about creating and uploading files to your server.
To create your own template, open up your WordPress theme of choice and locate the page.php file. This file is normally located in the root directory, so finding it shouldn’t be too hard. Then proceed to make a copy of the file, giving it a name of your liking; it’s best to give it a descriptive name such as template-custom.php, in order to ensure it won’t conflict with other templates.
Now you need to open the file, and add the following header at the very beginning:
<?php /* Template Name: My Custom Template */ ?>
Here, you can replace My Custom Template with any name you wish. This little header will tell WordPress that the file is a page template, and as such it will be included in the list of available page templates on the edit screen. From here onwards, you can customize this template as you see fit,and all pages using it will reflect these customizations. You can do just about anything here, from making simple changes to the page’s structure, to making custom queries to display content dinamically.
Upload Your Template
Once you’ve created your file, you can then proceed to upload it to your WordPress site. you can do this in a number of ways:
- You can use a FTP client like FileZilla and upload the file manually. The file should be located in your current theme, so all you have to do is navigate to wp-content/themes/YOURTHEME/, and upload the file. As soon as it’s present in the directory, WordPress should recognize it right away.
- You can add it to your theme files and then upload everything in one go using the WordPress theme upload feature. While this is a simpler solution, it requires you to delete the existing theme in your site, as WordPress won’t let you overwrite the theme folder.
- For the more advanced users, you can package it in a plugin so it becomes update-safe. However, considering the page template may only work with your current theme (since it follows its styling), it might be better to simply add it to the theme’s directory.
And that’s it. It is worth noting that page templates are part of the visual appearance offered by a theme, so it’s alright to include them in your current theme’s directory. If you wish to protect those templates from being erased when updating your files, you can always use a child theme to do so.
Create Page Templates
Page templates are an awesome feature in WordPress, and you should definitely take advantage of it. Depending on your needs, you can use them to create unique per-page designs, add custom content, or include alternate layouts. The possibilities are endless. Overall, they are a great step into the more advanced aspects of WordPress customization, and worth getting to know them better.