Posted on 1 October, 2008 By Kim Woodbridge 48 Comments

WordPress – How to List Recent Posts from One Category

There was an error in the code example that has been corrected. The code will now work in a sidebar text widget.

Categories (by Drift Words)

At the bottom of the sidebar on my other pages like About, I have a list of my recent WordPress posts. Go ahead, go take a look and then come back – I’ll wait.

This is easily done by adding a loop to the sidebar and a query that locates posts from just one category.

Please note that this will only work with standard sidebars. If you are using widget ready sidebars you will want to install the Exec-PHP plugin and then add the code to one of the text widgets. Exec-PHP allows you to run PHP code in posts, pages and widgetized sidebars.

<?php query_posts('category_name=wordpress&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>  </li>
        <?php endwhile; ?>

The main part of this is the standard WordPress loop that you will find on most of your template files.

<?php while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>  </li>
        <?php endwhile; ?>

What differs is the query_posts code right before the loop.

<?php query_posts('category_name=wordpress&showposts=5'); ?>

The query_posts code is telling WordPress to locate my last 5 posts in the WordPress category. The loop then runs the code to display them.

This is a great way to highlight the posts in your strongest or most popular category rather than just using a list of all the recent posts.

photo credit: drift-words


Related Posts:
  • (Anti) Social-Lists 9/13/09
  • Using Thumbnails for Excerpts Plugin with Query Posts to Make a Custom Sidebar Block in WordPress
  • WordPress How To: 5 Tips and Tricks
  • How to Exclude a Category From the Sidebar List in WordPress
  • Random WordPress Quotes Without A Plugin
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    48 Responses to “WordPress – How to List Recent Posts from One Category”

    Trackbacks/Pingbacks
    1. 10 WordPress ‘HOW-TO’ to Give it the Quality it Deserves
    2. (Anti) Social-Lists 9/13/09 | (Anti) Social Development
    3. FAQPAL Blog
    4. 23 articles and resources to improve your Wordpress life! | JortK.nl
    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • Christine: This has been driving me crazy for... maybe forever! I created too many lists and deleting them was not exactly clear -...
      • Ching Ya: Honestly I can't think of a way to live without lists. :-) Twitter lists, Facebook lists.. all of these have helped me a...
      • Kim Woodbridge: Hi Pizza - I guess people have different tastes :-)...
      • Kim Woodbridge: Hi JesC - Great! I'm glad it helped. I had found other tutorials that were hard to follow so I wrote this one up....
      • Kim Woodbridge: Hi Bhagu - Great! I'm glad it helped....
      • Pizza: These are some nice templates, and thank you for sharing but... those buttons on vegetable are UGLY as sin, I mean who t...
      • Jim: hahahaha YES!...
      • Mrs Moody@video tutorial blog: Hi Kim, first time for me in your blog, thanks for this post, lol it's funny and will definitely try it in facebook, gre...