How many pages did you create in your last WordPress project? If you're using WordPress as a content management system and not for blogging or any other reason, it's very possible that pages are the most used post type in that project. Why? Because pages are the most basic and most useful post type in WordPress.
There are five default post types that come with WordPress out of the box: posts, pages, attachments, revisions, and navigational menus. Arguably, pages have the most importance among these built-in post types. It's extremely common for a corporate website to consist of many pages, and you can see tens, maybe hundreds of different pages in a website like that.
Although pages are very important for WordPress as a content management system, it's easy to make a very boring website, with pages identical to others. That's where "page templates" come into play: Page templates are probably the most effective way to spice up your pages' designs.
What Are Page Templates?
In essence, page templates let you customize the look and feel of your pages. You can't serve them as a plugin, but you can use them in your themes—or child themes. By creating and placing them in your theme's folder, you'll be able to use different layouts for your pages automatically or optionally, depending on the type of page template.
Yes, there are different types of page templates. There are three kinds, in fact:
- The default page template, which is the
page.phpfile of your theme
- Specialized page templates, which are literally specialized for specific pages
- Custom page templates, which are the ones that we all think of when we see the words "page template"
The default page template,
page.php, is the file that overrides
index.php in order to change the design of your pages. If you want to design a new layout for a specific page (for example, the page with the "about" slug), you can use specialized page templates which override both
index.php. And if you want to create a new page design to use in any page you want, you can use custom page templates which override specialized page templates,
I don't think it's necessary to look into the
page.php file any further, so let's move on to specialized page templates.
Specialized Page Templates
Specialized page templates are the ones that can be set for a single page and force it to use a layout, instead of leaving it to the user's decision. It's a good way to set page templates for specific pages when designing a website for a client (or yourself), but it's not as useful as custom page templates.
There are two very easy ways to create specialized page templates: by using the page's ID or its slug. Simply naming your template file with the ID or slug of the page, like
page-about.php, forces WordPress to use that template to show the page. (Slugs have more priority than IDs in specialized page templates, so
page-about.php will override
page-9.php if they're both intended for the same page.)
If you're making a theme for the public, you shouldn't use specialized page templates unless you have a very specific reason to do so.
Custom Page Templates
As I said before, custom page templates are the kind of page templates that everyone thinks of when "page templates" are mentioned, because of their ease of use and consequent popularity.
Creating custom page templates is also very easy. You just put the following piece of PHP comment at the beginning of the template file and WordPress takes care of the rest:
<?php /* * * Template Name: My Template * * (You can also use other lines before or after the line above, * WordPress only cares about the line that starts with "Template Name".) * */ ?>
That's it! Now in the editing screen of each page, you can select this custom page template instead of the default page template.
If you're going to make and release a theme, keep in mind that custom page templates are one of the best ways to enrich a theme, and you're practically expected to create a couple of them.
Useful Tips and Tricks
Creating a specialized or custom page template: This one's a no-brainer—if you want to create a specialized or custom page template, just duplicate the
page.php file, rename it with a name of your choice, and edit the file as you like.
Organizing page templates in a subfolder: Here's a fun fact: You can store your custom page template files in a subfolder, instead of dumping them all to the root folder of your theme. (This does not apply to specialized page templates.) Just keep in mind that a child theme also needs to have the same subfolder if it intends to override the custom page templates in the parent theme.
If you don't want (or need) to have a subfolder, you should at least name your custom page template files with a prefix (like
page-template-***.php) to increase their visibility among other theme files.
Using custom templates in other post types: Sadly, the liberty of using custom templates isn't possible for post types other than Pages. You can set a generic page template for your Portfolio post type by creating a specialized template file called
single-portfolio.php but you can't set different custom templates for each portfolio item. In order to achieve it, I found an old (and possibly abandoned) plugin called Custom Post Type Page Template. It still works nicely in WordPress 4.0, so you might want to give it a shot if you really need this kind of functionality.
Naming your custom page templates right: If you're developing a theme to release it, you should think from the perspective of all users, not just you. Naming custom page templates is just one example: If you want people to use your theme without any hassle, you should pick your custom page template names carefully. You might understand what "1/1" means, but you must name it to "Full Width Page" to prevent any confusion. Your users might even just skip using custom page templates altogether if they don't understand what they're about.
Compared to other features, page templates have a a very simple logic, and yet they show us the richness of WordPress (in terms of design) more than any other WordPress feature. I personally love them, and looking at the variety of custom page templates in the most popular free and commercial WordPress themes, I can say that the community loves them as well.
What do you think about page templates? Do you have any different ideas, opinions, or things I missed in this tutorial? Tell us what you think in the comments section. And if you liked the article, don't forget to share it with your friends!
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post