Page template usage in WordPress

How does a normal WordPress page look like? Header, content, sidebar and footer. What if you want a different layout for a particular page or a set of pages. A different sidebar or multiple sidebar or no sidebar at all? Different styling and arrangement of other things (purely CSS based)? Anything different from the regular layout can be incorporated within WordPress using Page templates.

For example : A regular WordPress page generated from page.php of your theme :

<?php get_header(); ?>
    <div class="normal_page">
        <div class="content">
            <!– Content comes here –>
        </div>
        <?php get_sidebar(); ?>
    </div>
<?php get_footer(); ?>

For a special page, you don’t want any sidebar. For which the code of the page looks like :

<?php get_header(); ?>
    <div class="normal_page">
        <div class="content">
            <!– Content comes here –>
        </div>
        <div class="advertisement">
            <!– Ad comes here –>
        </div>
        <div class="hire_me">
            <!– Details on hiring me –>
        </div>
        <?php get_sidebar(); ?>
    </div>
<?php get_footer(); ?>

Creating a Page template in WordPress

Just before the code of that special page, add these lines

<?php
/*
Template Name: Special Page
*/
?>

so that the whole page template file looks like this :

<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
    <div class="normal_page">
        <div class="content">
            <!– Content comes here –>
        </div>
        <div class="advertisement">
            <!– Ad comes here –>
        </div>
        <div class="hire_me">
            <!– Details on hiring me –>
        </div>
        <?php get_sidebar(); ?>
    </div>
<?php get_footer(); ?>

That’s it. Your page template is ready to be used. Just drop the file in your theme folder with any name. However I would recommend giving the template name to the file name too so that it becomes easy to recognize it.

Using a Page template in WordPress

Now when you are creating a new page, you can select the page template under the Attributes tag on the right side.wp-cms-select-page-template-1

You can also make the page use by quick editing the page under Edit pages page.wp-quick-edit-page wp-quick-edit-page-template

Go fire up your creativity. Now you can have different layouts for different pages. Have any questions then use the Comments section below. Make sure you check out the whole WordPress as CMS series.


Comments

2 responses to “Page template usage in WordPress”

  1. […] is a good chance to understand how they work and how you can use them. Read this article – WordPress page templates and come back to this […]

  2. […] How to use a page template […]