How to Feature Your Best Posts in Your Sidebar

Looking for a way to feature some of your better posts? Here’s a method I’ve been using for a few months (visible on the index and the screencap to the right).

The five most recent posts that I’ve marked as “Featured” will appear in the list, along with a “view all” option that takes the clicker to a custom date-based archive.

How does it work? It’s done with multiple loops and categories.

Creating the Sidebar List

  1. Create a new category called “Featured.” Note its ID (mine is 72, so I’ll use that throughout this tutorial).
  2. Add a few posts to the category.
  3. Paste the following code into your sidebar, where you want the list to appear.


<ul>
<?php
$postslist = get_posts('category=72&order=DESC&numberposts=5');
foreach ($postslist as $post) :
setup_postdata($post);
?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
endforeach;
?>
<li><a href="http://www.webmaster-source.com/featured/">[View All]</a></li>
</ul>

Be sure to replace the bold “72” with the ID of your category. Seeing as this is another loop, the code must be located below your main loop (which means your get_sidebar() command must be below the main loop in your template). If your template’s sidebar is in the wrong place, you could use the $wpdb->query() function. However, I will not provide pasteable code, as most themes’ sidebars are below the main loop (and it’s much better if it is, as your content will be higher up, which is better for search engines).

Creating a Custom Archive

Create a new file called featured.php. This will be your template for the archive. Upload it to your theme directory, after adding something like this into it:


<?php
/*
Template Name: Featured Posts
*/
?>


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

<h2>Featured Posts</h2>

<h3>2008</h3>
<ul>
<?php
query_posts('year=2008&category_name=Featured');
if (have_posts()) :
while (have_posts()) : the_post();
?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
endwhile;
endif;
?>
</ul>

<h3>2007</h3>
<ul>
<?php
query_posts('year=2007&category_name=Featured');
if (have_posts()) :
while (have_posts()) : the_post();
?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
endwhile;
endif;
?>
</ul>

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

You’ll have to modify it a little so it will work with your theme. Note that you have to add a loop for every year you have posts (every new year, you need to put a new one in).

Create a new Page (Admin->Write->Page) and call it “Featured Posts” (or something of the sort). Set the Post Slug to “featured” and the Template to “Featured Posts.”

Load yourdomain.com/featured/ into your browser to make sure everything works.

Note that this isn’t the only way to create the archive. You could just use the default, at yourdomain.com/category/featured/, or you could use $wpdb->query() to create a more advanced archive. It’s up to you, as well as your experience level. $wpdb->query() is generally the domain of plugin authors, and the large majority of bloggers should probably steer clear.

Well, enjoy your new Featured Posts system.