Quick Tip – Separate Page Templates in WordPress
Sometimes not every page on a blog can fit the same “About Me” style.
Making unique page templates is surprisingly simple to do in WordPress, and is often an overlooked feature. There’s no need for bloating a single file with series of PHP loops to determine the file name and content. This is built in simplicity, and we’ll start with a brief explanation of how it works.
How WordPress Handles Templates
Put simply, WordPress handles templates in a hierarchy. Each page loads from a different base template file based on the type. A blog post loads from single.php, category view from category.php, and pages load from page.php by default.
This is where WordPress becomes awesome all over again. It supports and recognizes multiple variations of page templates as long as the prefix is “page-”. If you’re lost, the diagram below from the WordPress Codex should help explain.
This hierarchy of file names opens up a number of exciting possibilities for themes. A similar concept is applied within the specific category ID’s, so each of your categories could have a unique look. A photos section could appear as a thumbnail gallery while a text section might just show lists. The result would be impressive but also easy to manage since it remains within the WordPress engine.
Make a Custom Page
So as you may have picked up from the section above; making a custom page template is easy. You don’t even have to know any fancy PHP to do it. It’s simply a matter of making separate page file, renaming it, and then assigning a display name in the opening comments.
Create a new file called “page-news.php” which will be the basis for our new community link feed. Once the file is created in the blog theme’s root directory, we’d just have to past in the following comment in order to have WordPress fully recognize it.
Template Name: Community News
How’s that for easy? From this point the custom page template can now be designed independently. None of the code written in the default page.php will be loaded as long as the custom file exists. Now that we’ve got the file made, we can go into the dashboard for the rest.
Go ahead an make a new page or open an existing one in the dashboard. I’m running version 2.8 at the time of publishing, so in the right column of the page editor I see the following series of options in the “Attributes” block:
If we look at a closeup of that box we’ll see that there is a drop down box label “Template” in the middle. This is where we activate any custom page templates from. The name loaded here is the one we place in the opening comment lines of the page-customnamehere PHP file. In this case it will be called “Community News”.
Enjoy Your Fresh Start
And that’s it! This kind of theme development makes redesigning much more segmented and therefore easier to do over time. Do you know any WordPress themes out in the wild that use page templates effectively? Post a link below to share!