As requested by some attendees at the WordCamp Toronto 2009, I've uploaded my presentation slides: "Various Ways of Using WordPress." The slides can be downloaded at SlideShare and I've also embedded them in this post. If you missed the event, this post is a quick recap of my presentation. You will learn how I use WordPress to manage my sites: Web Designer Wall (blog), Best Web Gallery (gallery), and IconDock (eCommerce/blog). Get ready and learn more about WordPress theme coding.

Things You Should Know:

Example 1: Using WordPress As A Blog (WebDesignerWall)

Displaying A Post Image Using Custom Fields

The following sample code will show you how to use custom field to display a post image as seen on Web Designer Wall.

Custom post image

To add a custom field, go to Admin > Write a new post, scroll down to find the Custom Fields panel, enter the custom field name (Key) and the Value (the URL of the image location).

Custom field

Open the theme index.php file, in between The Loop, enter the following code where you want to display the custom post image.

<?php $postimage = get_post_meta($post->ID, 'post_image', true); ?>

<?php if ($postimage != "") { ?>
  <a href="<?php the_permalink() ?>"><img src="<?php echo $postimage; ?>" /></a>
<?php } ?>

Displaying a Dynamic <title> Tag

The following sample code will show you how to display a dynamic <title> tag depending on the page the visitor is viewing. For example: if it is home, display the blog name; if it is 404, display "404 Not Found"; and so on.

<title>
  <?php if (is_home()) {
    echo bloginfo('name');
  } elseif (is_404()) {
    echo '404 Not Found';
  } elseif (is_category()) {
    echo 'Category:'; wp_title('');
  } elseif (is_search()) {
    echo 'Search Results';
  } elseif ( is_day() || is_month() || is_year() ) {
    echo 'Archives:'; wp_title('');
  } else {
    echo wp_title('');
  }?>
</title>

Example 2: Using WordPress As A Gallery (BestWebGallery)

Managing Posts With Custom Fields

There are three custom fields: thumb, large image, and url.

Custom fields

To make life easier, I use the plugin Custom Write Panel to manage the posts. With this plugin, I can create a custom write panel where all custom fields are in one panel.

Custom write panel

Theme Switcher

I use the Theme Switcher plugin to allow visitors to choose their layout preference: thumbnail, large preview, and details mode.

Theme switcher

Below is the overview of the themes. The "master" theme is the thumbnail mode which is the default theme. It has most of the template files: CSS, JS, images, and PHP files, etc.

Theme overview

Since the header, sidebar, footer, page, 404, and single template file are the same, I use PHP include to get the files from the "master" theme.

Below is a sample code of the "full" and "details" theme index.php file.

<?php if (is_page()) {
  include ('./wp-content/themes/master/page.php');
} elseif (is_404()) {
  include ('./wp-content/themes/master/404.php');
} elseif (in_category(8)) {
  include ('./wp-content/themes/master/category-8.php');
} elseif (is_single()) {
  include ('./wp-content/themes/master/single.php');
} else {?>
   
  <?php include ('./wp-content/themes/master/header.php'); ?>
    <div>. . . display posts (The Loop here). . . </div>
  <?php include ('./wp-content/themes/master/sidebar.php'); ?>
  <?php include ('./wp-content/themes/master/footer.php'); ?>
  
<?php }?>

The style.css file in the "full" and "details" theme is used for the theme naming purpose.

/*   
  Theme Name: Details Mode 
*/

Example 3: Using WordPress As A Shop (IconDock)

Displaying The 5 Latest Posts

The following tutorial will demonstrate on how to display the 5 latest posts as seen on the homepage of IconDock.

Display 5 latest posts

I use query_posts to get the 5 latest posts from the database. Notice I have a "loopcounter". If the counter is less than or equal to 1, display the full content (which is the first post). Else, display the last 4 posts in a <ul> list.

<?php query_posts('showposts=5'); ?>  

<?php if (have_posts()) : ?>  
<?php while (have_posts()) : the_post(); $loopcounter++; ?>  
  <?php if ($loopcounter <= 1) { ?>  
  <div> first post content </div>
  
  <ul class="recent-post">  
  <?php } else { ?>  
    <li> last 4 post links </li>  
  <? } ?>  
<?php endwhile;?>  
  </ul>  
<?php else : ?>  
<?php endif; ?>

Conditional Tags

On the IconDock blog page, notice there are two version of post data (under the blog post title)?

Conditional tags

I use the Conditional Tags to check what category the post is stored in. If the post is stored in category 28 (which is the free icon category), display the credits and rating stars. Else, display the regular post data.

<?php if (in_category('28')) { ?>  
  <p>credits</p>  
  <?php if(function_exists('the_ratings')) { the_ratings(); } ?>  
<?php } else { ?>  
  <p>regular post data</p>  
<? } ?>

Managing Free Icon Posts With Custom Fields

Again, I'm using custom fields to manage the free icon posts.

Free icon posts

The plugin I use to manage the posts is Flutter. It is basically similar to Custom Write Panel, but for newer version of WordPress.

Flutter

Getting Rid Of The Category Base

By default, WordPress requires a category base in the URL structure. To get rid of that, create a blank Page and a custom Page template to query the posts from the category. Visit the Icon Blog and Free Icons page to see the result.

Below is a sample code of a Page template. I use query_posts to get posts from category 28, which is the free icon category. I set the posts_per_page -1, this means get all posts.

<?php
/*
Template Name: Template - Free Icons
*/
?>

<?php get_header(); ?>

<?php $page_num = $paged;
if ($pagenum='') $pagenum =1;
query_posts('cat=28&posts_per_page=-1&paged='.$page_num); ?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
  <div> display post here </div>
<? endwhile; endif; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Then create a blank Page and assign the Page template to it.

Page template

WP E-Commerce Plugin

The shopping cart at IconDock is powered by WP E-Commerce plugin. Head over to Instinct website for more details or download the plugin.

WP E-Commerce

The current version I'm using now is modified by the developers at Instinct. I've heard they are working on a template engine which will be release soon. The template engine will work similar to the WordPress template tags. So, look out for their new releases.

More WordPress Examples

Here are five great examples of using WordPress for different purposes.

45royale Inc.

WP E-Commerce

Creative Depart

Creative Depart

Typographica

Typographica

FlickOut

FlickOut

Jeff Finley

Jeff Finley

As promised at the conference, below is the code that Jeff Finley use to display the related posts with custom field. The plugin he used is Yet Another Related Posts Plugin. This code should go to the single.php file.

<?php $results = $wpdb->get_results(yarpp_sql(array('post'),array())); foreach ( (array) $results as $_post ) : ?>

  <a href="<?php echo get_permalink($_post->ID); ?>"><img src="<?php echo get_post_meta($_post->ID, 'post_img', true); ?>" /></a>

<?php endforeach; ?>

Presentation Slides