Thursday, January 31, 2013

WordPress Basics - Creating Custom Pages

WordPress is based on the use of templates for specific purposes. There are templates for different types of situations, like a blog posting, a regular page, a 404 error or to display blog postings for a certain category. The default templates are “single.php”, “page.php”, “search.php”, “author.php”, “404.php”, “category.php”, “archive.php” and “index.php”. There is a hierarchy to all these template pages which follow an order depending on what the visitor wants to see.
Whenever you are in the WordPress admin and you’re creating or editing a page or blog post, you will see what template is currently being used:
Selecting WordPress Default Template
The “Default” will either use the “single.php” template page for posts or will use “page.php” for pages (WordPress 3.0+). You can add your own template relatively easy, below are the steps to creating a custom template.
  1. Create a template file
  2. Customize your template file
  3. Apply your template to the desired page

Creating a template file

To create a template file you will need to first create a php file in your themes root directory:
wp-content/themes/mytheme/
At the very top of the php file you will need to name your template page. For example, if I wanted my “services” page to have a different layout I would write the following at the top of my template page:
<?php
/*
Template Name: Services
/*
?>

Next, save the file and name it something that is easy to associate with, like “services-template.php”. The next step is customizing the layout of the page.

Customizing your template file

To customize your template file you will first need to, at least, know what the purpose will be. Will it be to show dynamic content (like the a blog posting) or static content like most pages are used to display.
You can look at the other template pages in your theme directory to get some ideas (page.php or single.php).
The default page templates will start with:
<?php get_header()?>
This will appear right after the template name at the very top. You can either use this code snippet to keep the same header as the other template files or create your own header. If you will only wish to change your header slightly from the other pages, just copy the header contents (from header.php) and alter what you wish.
For the rest of the page you can either use create your own static content (using XHTML and CSS) or use a loop to display content added through the WordPress admin.
To display content added through the WordPress admin, simple copy the loop section from the default page (page.php). See below (using the default TwentyTen theme):
<div id=”container”>
<div id=”content”>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id=”post-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class=”entry-title”><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class=”entry-title”><?php the_title(); ?></h1>
<?php } ?>
<div class=”entry-content”>
<?php the_content(); ?>
<?php wp_link_pages( array( ‘before’ => ‘<div>’ . __( ‘Pages:’, ‘twentyten’ ), ‘after’ => ‘</div>’ ) ); ?>
<?php edit_post_link( __( ‘Edit’, ‘twentyten’ ), ‘<span class=”edit-link”>’, ‘</span>’ ); ?>
</div><!– .entry-content –>
</div><!– #post-## –>
<?php comments_template( ”, true ); ?>
<?php endwhile; ?>
</div><!– #content –>
</div><!– #container –>
You can comment out any areas that you don’t want to display, like the comments section, or you can simply remove sections that don’t apply to your template page. For example, if your custom page will never be used for the home page you won’t need the following:
<?php if ( is_front_page() ) { ?>
<h2 class=”entry-title”><?php the_title(); ?></h2>
<?php } else { ?>
After you have completed the page customization of the main body, you can determine if the page will have the same sidebar or footer as your other pages. By default, the bottom of every blog post or page ends with:
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You may not need a sidebar for this particular “Services” page but wish to keep the footer the same, you can just comment out the sidebar area (you may wish to add it later).
<?php // get_sidebar(); ?>
<?php get_footer(); ?>
You can always comment out both the default sidebar and footer snippets and create custom ones.
That’s basically it! You’ve created a custom template page for your WordPress theme.  The next step is to actually apply it to the page(s) of your choice.

Apply your template to the desired page

This template name “Services” will now appear in the drop down selection under the “Page Attributes”. (after uploading the template page to your online server).
Selecting Services as a template
Simply apply the template and you’re set.

No comments:

Post a Comment