Time for action – creating a custom page template

Imagine that you have some pages containing a lot of content which you want to display across the full width of the page, without the sidebar getting in the way. The way to handle this is to create a page template which doesn't include the sidebar, and then select that page template when you're creating or editing those pages. Let's try it out.

  1. In the same folder as your other theme files, make a copy of your page.php file and call it page-no-sidebar.php.
  2. At the very top of the file, above the line reading <?php get_header(); ?>, insert the following code:
    <?php
    /*
    Template Name: Full width page without sidebar
    */
    ?>
  3. Find the following line of code:
    <div class="content left two-thirds">
  4. Edit it so it reads:
    <div class="content left full">
  5. Now find the line that reads <?php get_sidebar(); ?> and delete it.
  6. Save your file.

What just happened?

We created a new template file called page-no-sidebar.php, and edited it to display content differently from our default page template:

  • We edited the classes for our .content div, using the object-oriented approach to styling used by the layout-core.css file. This will apply styling for the .full class to this div, so that it displays a full width instead of two-thirds of its containing element.
  • We removed the line calling the get_sidebar include, so that it won't be displayed on any pages using this template.

The lines we added at the top are essential for WordPress to pick up on our page template and make it available in the WordPress admin. Page editors will see a drop-down list of page templates available, and the name we defined in the template is what they'll see in that list, as shown in the following screenshot:

What just happened?

As you can see, in the Page Attributes box to the right-hand side, a new select box has appeared called Template. Our new page template is listed in that select box, along with Default Template, which is page.php.

Now we can try it out by assigning this template to a page and seeing how it looks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset