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.


16 Responses to “How to Feature Your Best Posts in Your Sidebar” (Comments RSS)

  1. Trackback: Update: Double Your FeedBurner RSS Numbers Overnight | So You Want To Teach?
    7:54 am on January 29th, 2008

    […] How to Feature Your Best Posts in Your Sidebar Subscribe to this blog by clicking on the orange button on the left.Click the StumbleUpon link below to share this blog with the world!Thank you so much for visiting So You Want To Teach? Your very first stop should be the Welcome Center.StumbleUpon […]

  2. Trackback: DBT Group Project: Some Tutorials Worth Reading » Webmaster-Source
    8:43 am on January 29th, 2008

    […] what post I submitted? How to Feature Your Best Posts in Your Sidebar, which I wrote not long after seeing the contest. It was in my mental list of “Things That […]

  3. Trackback: Blog Writing Project: 5 Best Tutorials
    1:23 am on January 30th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  4. Trackback: 4 tips to increase your Feed Readers (written by DailyBlogTips Writing Projects Participants) | JackBook.Com
    4:48 am on January 30th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  5. Trackback: 4 tips to increase your Feed Readers (written by DailyBlogTips Writing Projects Participants) | JackBook.Com
    4:48 am on January 30th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  6. Trackback:   Blog Writing Project — Tutorials — Wayfarer Wings
    4:14 pm on January 30th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  7. Trackback: My 3 Favorite Tutorials From Daily Blog Tips
    5:09 pm on January 30th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  8. How To Tutorials
    5:03 am on January 31st, 2008

    How do I do this if I am using widgets for the sidebar?

  9. Matt
    12:27 pm on January 31st, 2008

    I don’t use widgets myself, but I think this may help you out: http://bluesome.net/post/2005/08/18/50/ The Exec PHP plugin has a feature that should allow the code snippet to execute inside a Text Widget.

  10. Trackback: My Picks for DBT's Jan Group Writing Project
    1:35 pm on January 31st, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  11. Trackback: My top 5 picks from Daily Blog Tips Writing Project
    1:54 pm on January 31st, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  12. Trackback: Os meus 5 melhores - Daily Blog Tips Writing Project - Blog do TioSolid
    7:06 pm on January 31st, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  13. Trackback: Blog Writing Project: Tutorials ยป My top 5
    3:38 am on February 1st, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  14. Trackback: How to Write a Memoir to Remember Nominated for Best Tutorial
    3:55 pm on February 2nd, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

  15. Franca Richard
    9:05 pm on April 11th, 2008

    Great php loops, i love them, I ll try to set up my featured-post page soon!

  16. Trackback: Blog Writing Project: 71 Tutorials for Your Delight
    2:26 pm on April 18th, 2008

    […] How to Feature Your Best Posts in Your Sidebar by Matt. […]

Leave a Reply (or trackback)


Close
E-mail It