Create a custom template in Wordpess

1. Open up a new file and add the following:

<?php
/*
Template Name: My Template
*/
?>

2. Save the file in your theme folder with the name of your new template, the filename doesn’t have to be an exact match for the template name, e.g. ‘mytemplate.php’.

3. In the admin panel create a new page. Give it a title, type in some content, and then in the right sidebar under ‘Attributes’, find your new template in the drop down and select it, and publish the page.

4. When you view your new page on your sites front-end you’ll see a totally blank page. This is good and means it’s worked as we’ve not told the template to do anything yet.

5. Go back to your template file and update it to match the following:

<?php
/*
Template Name: My Template
*/
get_header();

get_footer();
?>

6. Now when you refresh your new page you’ll see the header and footer are now incorporated:

7. That’s great but the content from the CMS is still missing. Add the following into the template file between the get_header() and get_footer() calls, save it and refresh the page:

$thisPage=query_posts("pagename=".$_SERVER["PATH_INFO"]);
echo $thisPage[0]->post_content;

That’s it, you’ve just created your own template! You can now continue to develop out your new template and create more pages using that template, and create more templates.

This is part of an ongoing series of posts I am publishing talking about how to use Wordpress as a custom CMS.

Tags: CMS, PHP, WordPress

One Response to “Create a custom template in Wordpess”

  1. Jon says:

    This may come in useful during the next week or so as I think we’re just about to use Wordpress at my work for a few blogs. Thanks.

Leave a Reply