Feedback Follow Me on Twitter!

Combining Featured Posts with the Standard Loop

I recently had a client who requested his blog display posts similar to GigaOm.com — one featured excerpt, three featured headlines, followed by the standard Loop and pagination:

gigaom

It’s a nice format that’s mostly easy to replicate, but I hit two small problems. First, the large blue “Featured” box was supposed to disappear once you begin paging (/page/2/), just like GigaOm. This required a conditional which said, “Show features if you’re on the homepage, but not on a paged page”:

<?php if ( is_home() and !is_paged() ) { ?>
<!--FEATURED STUFF-->
<?php } ?>

The second problem was that multiple query_posts on the same page disrupted the standard query, which also caused pagination to fail. In the end, what fixed it was an explanation I found in the WP support forum archives, written by the always-knowledgeable Kafkaesqui:

To get proper pagination with query_posts() we need to recreate it through the ‘paged’ parameter or query. Best way to do this is to ask WordPress for the “page” we happen to be on, and use that as our ‘paged’ value.

He suggested combining query_posts with this bit of code:

<?php
  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  ?>

Add it all together and you get a working index.php template that functions like GigaOm:

<?php get_header(); ?>
<div id="content">

<?php if ( is_home() and !is_paged() ) { ?> 

  <!--FEATURED EXCERPT-->
  <div id="feature">
  <?php query_posts('category_name=featured&showposts=1'); ?>
  <?php while (have_posts()) : the_post(); ?>
  <h2><a href="<?php the_permalink() ?>" rel="bookmark">
  <?php the_title(); ?></a></h2>
<p class="postmetadata">
Posted by <?php the_author_posts_link(); ?>
on <?php the_time('F jS, Y') ?>
<span class="commentcount">
<?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?>
</span></p>
  <?php the_excerpt(); ?>
  <?php endwhile; ?>

  <!--FEATURED HEADLINES-->
  <?php query_posts('offset=1&category_name=featured&showposts=3'); ?>
  <ul>
  <?php while (have_posts()) : the_post(); ?>
  <li>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
<span class="commentcount"><?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?>
</span>
</li>
  <?php endwhile; ?>
  </ul>
  </div>
  <!--END FEATURED-->
<?php } ?>

  <!--STANDARD LOOP-->
<?php
  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts("cat=-5&showposts=3&paged=$page");
  ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div class="post" id="post-<?php the_ID(); ?>">
  <h2><a href="<?php the_permalink() ?>" rel="bookmark">
  <?php the_title(); ?>
  </a></h2>
  <p class="postmetadata">
  Posted by <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y') ?>
  <span class="commentcount">
  <?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?>
  </span></p>
<div class="entry">
  <?php the_content('&raquo; Read More'); ?>
  </div>
  </div>

  <?php endwhile; ?>

  <!--PAGINATION-->
  <div class="navigation">
  <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
  <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
  </div>

  <?php else : ?>
<h2 class="center">Not Found</h2>
  <p class="center">Sorry, but you are looking for something that isn't here.</p>
  <?php include (TEMPLATEPATH . "/searchform.php"); ?>
  <?php endif; ?>
  </div>

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

Note: a “cat=-5 (or whatever)” parameter was added to the second query so that Featured posts would not display redundantly within the Loop.

19 Responses {+}

Trackbacks

  1. Weekend Roundup #18 » JaypeeOnline // Blogging News & Reviews
  2. Weekend Roundup #18 - PinoyPortal
  3. Weekend Roundup #18 - PinoyPortal
  4. How to modify the standard loop of posts in Wordpress | Ktulu.it
  5. When To Use Magazine-Style Themes For Blogs? | How-To | Smashing Magazine
  6. Customize your WordPress Theme | Neowster

Leave a Response