ben tedder : code things

Create a 'featured' blog in WordPress (with no duplicates)

In your category view in WordPress, wouldn't it be awesome to show a 'featured' blog per category? Here's how (after many hours of work!).

Step 1: Setup
First, make sure you have another category called "featured". So when you want to create a featured blog entry in, say your category of "travel", then you would select "travel" and "featured" as categories.

Step 2: Category.php
Now, go into category.php. From here, insert a bit of code just above The Loop. Here's the explanation: We are going to create a query that pulls all blogs with the category 'feature', and then narrow it down by the current category, and then limit the items to 1. The default with wordpress is to sort things chronologically, so the most recent article you categorized as 'featured' and in the "travel" category, will show up.

Next, we will create a variable called "$do_not_duplicate". This will store the ID of the post it pulls for the feature. It's important to store this ID, because then we'll want to make sure and skip over that ID when we pull the rest of our blogs up, so there are no duplicates.

Ok, here's the code:

  <?php $my_query = new WP_Query('category_name=featured&cat='.$cat.'&posts_per_page=1');
   while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; 
    echo 'featured: ', the_title();     
   endwhile; ?>

Step 3: Start the Loop (minus the already featured article)
Replace the standard Loop starter with the following code:

  <?php if (have_posts()) : while (have_posts()) : the_post(); 
        if($post->ID == $do_not_duplicate) continue; ?>
    /* rest of loop content as normal */
<?php endwhile; endif; ?>

What this does is use that $do_not_duplicate variable from above, and just passes over it when you pull the rest of your blog entries in.

Hope it works for you!

If you need more help, try checking out how to do multiple loops on