Creating a WordPress Theme — Part 2

In the previous part of this sub-series, we started taking the first few steps to creating a WordPress theme from a Gimp .xcf design.

Today, we will be looking at using multiple loops in our WordPress theme to separate featured (sticky) posts from regular posts on the home page of the site.

If you've ever worked on a WordPress theme before, you should be familiar with the loop. If you want a detailed explanation, check out the WordPress Codex. Basically, it's what's used to get all our posts and display them on the page.

It usually looks a bit like this:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    Show the posts.
<?php endwhile; else: ?>
    If no posts were found, show this.
<?php endif; ?>

We will make use of the following loop for both our "Featured Post" and "Latest Post" loops.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
<?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
This displays the current post's title, with an excerpt. Add this loop into the "Featured Post" box, and the "Lastes Posts" boxes.
However, we don't want to show every post in both boxes. To show only a featured post, add this code just before the "Featured Post" loop:
<pre lang="PHP">
    $sticky = get_option('sticky_posts');
    rsort( $sticky );
    $sticky = array_slice($sticky, 0, 1);
    query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

This is a slightly modified version of this little snippet, to only show one post marked as sticky.

But, you'll notice that the "Latest Posts" box also shows the sticky post. We will fix this by using query_posts once again.

Add the following line just before the "Latest Posts" loop.

<?php query_posts('caller_get_posts=1&showposts=3'); ?>

This resets the loop to the normal settings, and sets it to only show three posts in this loop.

Be sure to stick around and watch out for the next part of this sub-series, and as always, if you have any questions, don't hesitate to leave a comment!

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!


  1. November 10, 2009

    Thanks for these resources. I'm honestly thinking about creating a custom theme myself someday. If I do, I'll be drawing from these tutorials.

  2. November 10, 2009

    @Nicholas Cardot: Thanks. And I'm sure you'll find making a WordPress theme very easy to do, with all of the tools included in WordPress itself.