WordPress is a powerful platform to use, making it the most utilized blogging platform. One of the aspects that make it such a popular and customizable program is the custom page template. Page templates are custom files that will be put in your theme directory. When finished, you will be able to create a new page for your WordPress site and set the layout to your customized template. This can turn any ordinary page on your website from a simple page of content into a dynamic webpage.
We will take a look at how to setup a page template file and add it to the page you created. But what the page template actually does is up to you. Maybe you will display a photo album from last month’s Church potluck or create a page that displays all of the free files you have ever uploaded onto your website.
The new WordPress default theme, TwentyEleven, actually has two page templates in its install: Sidebar Template and Showcase Template.
Setting Up The Page Template
Follow these steps to
- Create a PHP file within your active theme. The file name is important to distinguish it from any other templates or basic theme files. We will name it yourpage.php.
- Open the WordPress page template in a PHP editor and copy the code below into it. Please note that the Template Name must be something specific. Also, put this code within <?php … ?>
[cc lang=”php”]/* Template Name: YourPage */
if(!is_page(‘YourPage’)) { // Do something }
[/cc] - Add the code within the if statement that will customize your page. Maybe this is where you pull the Flickr photos for your photo album or the file names and locations to display your free files.
- Login into your WordPress site to the administration menu and then go to your Pages.
- Create a new page with a title.
- Within the Page Attributes Widget, change the template from Default Template to YourPage (or whatever is the template name that you put in the file).
- Publish the Page.
Assuming that the code that you put in the if statement of the page is correct, when you visit that page everything will show up and you will have completed your first custom page template.
Jared Erickson says
Not sure why you would need to include “if(!is_page(‘YourPage’)) { // Do something }” since the structure of this page will only show if this page template is being used. that if statement would only make sense if used on other files in the theme like index.php:
HELLO THIS IS MY PAGE
Jared Erickson says
poo, it stripped my PHP in the comment.
if(!is_page(‘YourPage’)) { echo ‘Hello This is My Page’ }
Jeremy Smith says
This is a redundancy check in place in case you mislabel a page with the wrong page. Say the template is for a Portfolio page, but I apply it to our Contact Us page. This if statement prevents it from displaying the incorrect layout.
It is not required for a template to be used, but a good idea to consider. Thanks for bringing this up.
Tom McFarlin says
Hey Jeremy!
I may be misunderstanding the tutorial, but I don’t think you need the conditional statement in the page.
When you define a template with the /* Template Name: YourPage */ comment and apply it to your page, WordPress will only display what’s included in that template.
By that, I mean that unless you explicitly include something such as get_header() or get_footer() or even any content, you’re completely free to incude whatever you want.
Either way – dig the idea!
Tom
Jeremy Smith says
Thanks for the idea. You are correct that it is not necessary (see reply above). And if you do need to have specific header/footer changes, the if statement is not optional anymore.