官术网_书友最值得收藏!

Using page templates in your theme

Even though most of the WordPress themes use a unique page template, our favorite blogging engine allows us to create as many different page templates as we want.

Why always use the same page layout in your blog? Using different page templates will make your blog look a lot more professional.

In this recipe, you'll learn to create and use page templates.

Getting ready

To achieve this recipe, you only need a WordPress theme and a text editor. In the following example, we use the WordPress default theme. If you're using another theme, you'll have to adjust the code a bit to make it fit your theme HTML markup.

How to do it...

Let us start with creating a test page template and learn how to create more page templates in the following recipes.

  1. On your theme directory, create a new file and name it testpage.php.
  2. Open the testpage.php file in your favorite text editor and insert the following code in it:
    <?php
    /*
    Template Name: Test Page
    */
    ?>
    <?php get_header(); ?>
    <div id="content" class="narrowcolumn">
    <h1>My first page template, called <?php the_title(); ?>!</h1>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  3. Login to your WordPress Dashboard and go to Write | Page and give your page a title. You don't have to write any text in the body since it won't be displayed by the page template.
  4. Scroll down until you see a Page template link. Clicking on the link will open a drop-down menu from where you can select the page template to use. Select the new template, Test Page.
    How to do it...

    .

  5. Publish the page.
  6. Visit your page. You will see a basic page with only a title displaying My first page template, called XXX (the name you have given to the page).

Now, you know how easy it is to create and use page templates in your WordPress theme. This example isn't useful at all, therefore, let us see some really interesting uses of page template in the following recipe.

How it works...

The WordPress page template allows you to define special template layouts that can be assigned to pages.

主站蜘蛛池模板: 樟树市| 孟村| 鄂托克旗| 宜良县| 六盘水市| 济宁市| 来安县| 昭通市| 宣威市| 五莲县| 富民县| 诏安县| 襄垣县| 札达县| 高邑县| 专栏| 驻马店市| 娄烦县| 宜章县| 石嘴山市| 博白县| 应城市| 葫芦岛市| 建瓯市| 丽江市| 临高县| 秀山| 漳平市| 宜城市| 大冶市| 资溪县| 吴江市| 宝应县| 惠州市| 东台市| 三河市| 宁夏| 台北市| 山西省| 白朗县| 靖宇县|