Creating custom page templates in WordPress
One of the reasons I love WordPress is the vast range of unique configurations and customization options. Child themes and custom page templates create virtually limitless options.
WordPress lets you start with the same theme base, and through the wonders of child themes and custom coding, end up with two totally different websites from that same template. Then you can extend this even further by creating custom page templates within your theme.
What is a custom page template?
One of the most powerful ways to customize a child theme is with page templates. A page template is just a pre-designed framework telling WordPress how you want your content displayed for that page. You can have as many or as few templates as you want, and each template can be applied to multiple pages (or just one page).
Of course, not every change or tweak you make to a page needs to go in a custom template. Some changes are better handled by leaving the content in the default WordPress page editor and applying changes via CSS or on-page formatting. So when should you use custom page templates?
Pro tip: In each of the following sections, it’s not horrible if you create a custom template in a do not instance. Think of them more as “it’s not necessary”; in most of these cases, you would just be creating more work for yourself.
Think about what you need to change
Create a template when you want to change something that’s already included.
The most common example of this is when you are working with a theme that does not have a full-width page option. In this case, you can create a new page template (called “Full-Width Page” or something equally clever) and remove the get_sidebar() call. Along with some CSS tweaks, this allows you to now choose which pages display with a sidebar and which ones are full-width simply by changing the page template.
Sometimes, you’ll also encounter a heading, description line or other text that is hard-coded into the template. If you want to change this wording, it makes sense to create a new template in your child theme and change it there.
Do not create a template when there’s no need (i.e. simple layouts).
If the only changes you want to make to a page’s format include the size of the header, some padding around the images, or to center all of the text, there is no need to create an entirely new page template. Simple changes such as this can be accomplished easily (and more quickly and cleanly) with CSS.
Create a template when you want several pages to have the same layout.
There are times when you will have several different sections on your site, each with their own page, but you want the layout to remain consistent. This is a situation when page templates really shine. You can perfect your layout once, and then reuse it on as many pages as you’d like. Conversely….
Do not create a template when only one page will use the layout.
Now I admit, this one is a gray area. On one hand, a page template isn’t really needed for only one page (you could simply customize that page’s code). However, there may be times you want that layout to be harder to adjust (*cough* mess up *cough*), so I could see using a template for only one page. It’s included as a DO NOT simply because, well, it keeps things even.
Think about how the page will change
Create a template when the page layout isn’t likely to change.
Create a custom template when the page design is likely to remain constant for the foreseeable future and the only changes that will need to be made are to content itself. Providing custom fields for the end-user to update the content is ideal; they can simply edit or add new copy into the corresponding box, and the page template takes care of the formatting.
You can, of course, code the custom fields yourself, or you can use a plugin like Advanced Custom Fields to help with that part. This also ensures that frequent changes to the copy (or many different hands changing the page text) won’t accidentally disrupt the formatting of the page.
Do not create a template when you need flexibility in formatting.
Sometimes, a client wants to be able to change the design of the page frequently (“Can we move this image over there and put this slider above it instead?”). Your layout could be rather complex, or you are creating several different complex layouts for various pages. In these cases, consider a good visual editor instead of creating page templates. A plugin like Visual Composer is easy to use and allows for drag-and-drop changes to page design and layout much more easily than editing page template code.
Use templates when they make your life easier!
In some cases, coding custom page templates up front will make your life easier in the long run. If you’ve added some backend/hidden formatting to your page, especially if it includes nested or “empty” divs or whitespace, create a page template. WordPress will sometimes remove, truncate, or adjust spaces and divs automatically when switching between the visual and text editors. Placing your formatting in a template and applying that to the page eliminates this issue.
If your client/end-user/whoever will be applying content updates isn’t overly web- or code-savvy, using templates can save a lot of future hassle. Adding copy to a few boxes and clicking “Update” is much less stressful for someone unfamiliar with HTML than having to worry about headline tags or remembering to close divs.
Templates also excel at displaying dynamically changing data. If you’re displaying a page that is created by listing posts or custom post types, USE your templates! Especially when used with custom post types, templates can do awesome things. You can even create a different template to display each post type in a different way, without having to worry about how many posts you have or will have (since everything is generated dynamically).
What’s your favorite custom templating tip? Did we miss your big question on when to create a new page template?