Posted On: Friday, July 16th, 2010 By: Neal
Part of what makes WordPress so useful is the myriad of features it supports, and the high level of customization that it offers. When first starting out, it can be daunting to try and explore all of the options available to you! So here are 5 techniques that I found useful when I first starting using WordPress to build websites.
1. Page Templates
Page templates are great because they allow you to make a special type of page that fits your needs, and it’s really simple! All you need to do is place the following code at the top of your document:
<?php
/* Template Name: Your Template Name here */
?>
Everything else on the page behaves just like any other wordpress page. Once you’ve added the template code at the top of the page, save the page as a .php document and add it to your theme directory. Now, when you make a new WordPress page, you can select your template form the template drop-down menu. This is especially helpful if you need to create multiple pages that exhibit a special behavior. Here’s a very simple example:
Let’s say that I want to create a certain template that will display a specific graphic at the top of any page using the template. First I’ll put the template code at the top of my file:
<?php
/* Template Name: Portfolio */
?>
Often it’s good to start with some code to build off of when making your page template. I’m going to paste the code from the page.php file from the default template that comes with WordPress 3.0 (the template is called “TwentyTen”). Here’s the code as it is in the default wordpress theme with my added template code at the top:
<?php
/* Template Name: Portfolio */
?>
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
<div>
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div>' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span>', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is great as starting code, but I want to add my title image. So I’ll add my <img> tag into the code and place it right after the opening “content” div like so:
<?php
/* Template Name: Portfolio */
?>
get_header(); ?>
<div id="container">
<div id="content" role="main">
<img src="images/title_graphic.jpg" alt="Title Graphic" />
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Now all I need to do is select the Portfolio template from the Page Template drop-down menu:

Any page that has the Portfolio page template selected will show the image I included in the template code. The best thing about this technique is that its implementation is simple, but the level of complexity you can achieve is surprising. You can make pages that extend the functionality of WordPress, but that are very simple to edit for clients!
2. Showing posts from a particular category
Sometimes you want to be able to create a series of individual posts that all fall within the same category and show those posts all on one page. For example, you may have a list of employees for a company that you want to display on a page called “Our Team”. This is possible with the WordPress function query_posts().
Like many great WordPress features, this is a very simple function to use. I’m going to show you how to take the page.php file from the default WordPress theme (twentyten) and alter it to show only posts from a specific category. First Let’s take a look at our page.php code:
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
<div>
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div>' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span>', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is the code that controls the content we’re seeing. First, the checks to see if there are in fact any posts. Since this is the page.php code, the code will find the content for the page we’ve created. For example, if you create a page called “Our Team” and write the word “content” in it, this code will find the “Our Team” page and show the word “content”. The code that says “the_title()” and “the_content()” simply pulls the title of the post (or the page in this case) and the content from the post (or page).
We want to change what is displayed, however, by adding the “query_posts()” function before the code that looks for posts. Just as an example, I’m going to create a page that displays all of the posts from the category “Team Members”. Here’s the code:
<?php query_posts('category_name=Team Members'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
<div>
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div>' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span>', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
Now that we’ve added this code, the behavior of the page will change. Instead of using our “Our Team” page as the source of the page content, the page will now look to the category called “Team Members” and if that category has posts, its content will be displayed here. For every post in the “Team Members” category, another post will be displayed on the page. This can be extremely useful when you already have an existing posts page and you want another page that displays posts. On my website, for example, I have both a Blog page and a Work page, both of which display posts. Using this technique, I was able to show one set of posts on one page, and another set of posts on another page.
3. The is_page() Function
If you only need to add a small piece of additional content onto your default page.php file, there is a quicker and easier way than using Page Templates. By using the is_page() function and other related functions, you can easily insert small bits of code that will show up only on a specific page. Let’s go back to the earlier example, the one in which I wanted to show a title graphic on only certain pages. This time, we’ll accomplish this using the is_page() function.
Let’s go back to our page.php file from the default TwentyTen WordPress Theme. Let’s say this time, we only want to add a header image for the Portfolio page.
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php if(is_page('Portfolio')) { ?>
<img src="images/page_header.jpg" alt="Page Header Image" />
<?php } ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Now, when a user visits the Portfolio page, the code will execute the code and show the image at the top of the page. If you want to show the image on several pages, this is also possible. Let’s alter the code so that if we’re on the About page or the Portfolio page, we’ll see the image:
<?php if(is_page(array('Portfolio', 'About'))) { ?>
<img src="images/page_header.jpg" alt="Page Header Image" />
<?php } ?>
By using an array with several values, we can target multiple pages at once. Similarly useful are the is_home() and is_category() functions. In fact, many of the functions that begin with “is_” are quite useful and I highly recommend you look into them!
4. Clean Permalinks
I got this information from this wordpress site: Using Permalinks
It can be unsightly and frankly somewhat confusing to find that all of your WordPress pages are named after their id number and not their page/post name! There’s a quick fix for this! If you go to your WordPress dashboard and click on Settings>Permalinks, you can create a custom permalinks structure. I like to use the one mentioned on the Using Permalinks page I referenced above. In the Settings>Permalinks menu, click on “Custom” and enter the following into the textbox:
/%category%/%postname%/
Now all of your pages will have URL’s named after their page names, and post URL’s will be named after the category they’re in and the name of the post. A post in called “Post 1″ in the category “Posts” will have the URL: http://www.yourdomain.com/posts/post-1

Now isn’t that cleaner!? Looks muuuuch nicer now!
5. Using Custom Fields
Using custom fields can help add extra optional information to your WordPress pages! You can find the Custom Fields input under the content-entry box when creating a page:

If you click “Enter new” you can name a new custom value. For this example, let’s name the field “Mood:” and in the value area, we’ll type “Productive”.

Click “Add Custom Field” to finalize your field and add it to the page. Next we need to find a way to display the Custom Field data. We’re going to use the get_post_meta() function to accomplish this.
With the get_post_meta() function, we need to pass 3 parameters to the function. The first is the id of the post that contains the Custom Field. In this case, we just want the Custom Value from the current page. To get the ID of the current page, you can use $post->ID. Next, we need to specify the name of the Custom Field we want to pull data from. In this case, the Custom Field is called “Mood”. Finally, we can specify whether or not we want one value, or multiple. If we, for example, created two fields on our page called “Mood” with two different values, we could enter “false” as the final function parameter (or leave it blank) to return an array with both of the “Mood” values. However, we only want to show one mood at a time, so we’ll enter “true” for the last of the function parameters, specifying that we only want the first Custom Value, not multiple values.
<?php $mood_value = get_post_meta($post->ID, 'Mood', 'true'); ?>
<?php echo $mood_value; ?>
The above code will output the value of the “Mood” Custom Field. However, we want to make sure that if a Mood isn’t set, then no content will be shown. And if a Mood is set, then we want to display it in a stylish manner. So, let’s add a bit to the code:
<?php if(get_post_meta($post->ID, 'Mood', true)) { ?>
<?php $mood_value = get_post_meta($post->ID, 'Mood', true); ?>
<strong id="mood">Mood:</strong> <?php echo $mood_value; ?>
<?php } ?>
Now, the code first checks to see if the “Mood” field has been filled out. If not, nothing is shown. If the “Mood” field does contain content, then the page will show “Mood: ” followed by the value we entered in the “Mood” field. We’ll see something like this:
Mood: Productive
Using Custom Fields can allow you to easily add optional content to any post you want. It’s a simple way to add extra functionality to your WordPress pages.
Well, I hope this tutorial has shed light on some of the powerful tools available to WordPress users! Feel free to e-mail me with questions or comments on the Contact Page! Until next time…
Categories: Blog, PHP, Web Development, Wordpress