What You Need to Know About WordPress Custom Page Templates
There are many many ways you can change the look of a page on your WordPress site: you can change the content inside the WordPress content editor, you can change the CSS rules that affect the site, you can create a new file in your template hierarchy to correspond to the specific page, or you can use a theme page template designed specifically for that page.
The last one is what we’re talking about here. We’ll cover what theme page templates are, why you’d use them, and what you need to know to get started.
When you say “WordPress Theme Custom Page Template”, what do you mean?
There’s a huge terminology problem with this topic. As I hinted at above, there are a lot of things with the vague notion of “page” and “template” in the WordPress world that aren’t what we’re talking about here. Some people call a “WordPress theme” a “template”, some people think of all content types in their WordPress site as “pages.” And there’s also this thing called the “template hierarchy” that seems likely to be related, but isn’t really involved.
So what do I mean? I mean what is shown at right: you’re within the Page content type in WordPress, and when you click to single page, you see the little content box illustrated on the right (it’s also typically on the right side of your screen, under the “Publish” box), which offers you a set of “Template”s. These templates available are being populated from the files in your currently active theme’s folder (or its parent), and have a single distinguishing feature we’ll come to later. This is what we’re talking about here today.
How Does This Fit Into the Template Hierarchy?
I’ve written before, and I’ll likely do so again, about how the template — there’s that word again — hierarchy is really the key concept you need to understand to build or modify a WordPress theme. (Here’s my quick intro to it.) Almost everything else is a detail you can work out once you’ve understood the basic dynamic of the template hierarchy. The specific thing we’re meaning today, though, sits outside of the template hierarchy, and overrides any of those details. This is for when we want a user to make an archives page, for example, or something else, not when we want to define some basic behaviors for the page.
If a Custom Page Template was selected in the dropdown we showed above, that “template” file will be used. Full stop, no exception.
If a Custom Page Template was selected in the dropdown we showed above, that “template” file will be used. Full stop, no exception. If not, then the WordPress template hierarchy will look for a somewhat different way that someone might have tried to style a specific page — with a
page-custom-template-utilizer.php file. (The difference between the Custom template, and one with this
page-$slug structure is really only in interaction and interface.) If neither of those is found, WordPress will fall back to
page.php, if it exists, and if not we’d end up at the ultimate fallback,
It’s generally best when building anything related to interdependent systems to allow for ignorance and independence as much as possible.
If you’re wondering why you’d use this method as opposed to the numeric or slug one — in either case you’re creating a custom file in your theme, after all — the reason is that using custom templates gives you more flexibility and independence between your theme and your site’s database and content. For that reason, I’d recommend it in just about every situation when you’re unsure which to go with. With the numeric or slug-based
page-*.php structure, you’re forced to make your theme know about the specific way your WordPress database data is right now. It’s generally best when building anything related to interdependent systems to allow for ignorance and independence as much as possible.
So How Do I Make a WordPress Custom Page Template?
Now you understand what we’re talking about, why you’d want to use it, and the final question is how. Well, for better or worse, these custom templates are one of the easiest things to make.
Add to your theme — and it really should be a child theme unless there’s a very strong constraint making that impossible for you — a file with a name like
really-cool-awesome.php. It doesn’t really matter, as long as you don’t actually use a name that’s part of the template hierarchy, you’re golden.
Once you’ve got the file, you should start it like this:
<?php /* Template Name: Name To Appear In The Dropdown */ ?> This is my custom template.
And you’re basically done. As you can probably guess, the name that will appear in the “Template” drop down is what I cleverly called “Name To Appear In The Dropdown.” Just like your theme’s
style.css file, or a plugin’s main file, WordPress uses the information inside a comment to provide it with its information. Then the content of our pages will, in this case be the very basic HTML text “This is my custom template”.
A Great Reason To Use Custom Page Templates
Now if you’ve done this before, you’ll notice that the page above will have no styling, and look very very little like the rest of your WordPress site (unless you have the simplest theme imaginable). That’s because our file has one simple thing in it, and lacks functionality very common in all other theme pages. It has no
get_footer(), or The Loop, calls that all your other theme files likely do.
Sometimes you want a page that’s not much like the rest of your site, but still a part of your WordPress management experience. These custom templates are perfect for that.
But it illustrates one of the great powers of custom templates like these as well: sometimes you want a page that’s not much like the rest of your site, but still a part of your WordPress management experience. These custom templates are perfect for that. While you see them used also for archives pages, or (less commonly) contact pages, this ability is often overlooked.
And Finally, Some Practical Advice About Page Templates
When you’re looking to make a more seamless page as part of your theme — which chances are good you are — I’d recommend you take a different course than we’ve followed so far. Rather than make your own file from scratch, start by duplicating a working file that’s part of your existing template hierarchy —
page.php is the obvious choice — and then make the small number of changes you want to customize the specific page. And don’t forget to add that crucial comment to the top so you can choose your template. Go make awesome pages!
Image credit: Infrogmation