WordPress Templates

WordPress Templates

By Jonathan on March 1, 2013

I’ve been working with WordPress for a while now and I really don’t know where I would be without the use of template creation for custom pages. They are super simple to implement but give you huge control over your content, if you base it on the standard page.php that is in your theme.

First of all, creating the template file. In your theme root create a new PHP file, call this what you want, but I like to use a prefix so all my templates are together. Open up your new PHP file, and copy the below PHP code into it. If you are planning on using a lot of templates, I would recommend placing them in a folder. WordPress will search your theme folder for any Template files, no matter where they are located, or how deep they are in a file structure!

{code type=php}

{/code}

You can replace ‘Super Duper Template’ with whatever you want to call your template. Now, login to your website dashboard, and navigate to your pages. Select, or create a new page that you wish to assign the template to. If this is your first template in the theme, you’ll now notice there is a Template drop down in the Page Attributes.

As you can see, I already have a couple of other templates set up. Assign the Super Duper Template to the page you have chosen, update and go to that page. You’ll notice there is nothing there, that’s because it’s working. We need to call in some WordPress functions to get this back to how it was before. You can do this two ways;

First, You can copy the entire contents of your page.php file into the template file. This way you will get exactly, what you had before. However you’ll now have the freedom to move, delete, create and customise your elements.

Based on the twentytwelve WordPress theme this is what should look like now:

{code type=php}
get_header(); ?>






{/code}

You can make this much simpler if you would like to start from nothing. All that is needed is actually the get_header(); additional code. However Its best to use the get_footer(); as well, as some plugins use elements in the footer.

{code type=php}
get_header(); ?>


{/code}

That’s it! Just remember to assign your template to the pages you wish to use. You can also use templates in a different ways depending on what you would like to modify. If you view the WordPress Template Hierarchy you’ll see there are many different templates that can be created.