Posted on 1 October, 2008 By 129 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


web hosting by media temple

Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




129 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
  5. WordPress: List Posts For A Category | The OS Quest
  6. 10 WordPress ‘HOW-TO’ to Give Your Blog the Quality it Deserves – Just a blog :)
  7. 10 Tips to Make Your WordPress Blog Outstanding - JustAnART
  8. Popular WordPress Tips for Beginners
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> .

CommentLuv badge

About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Tanya: These are some great plugins! Will have to try a few out, Thanks :) Love that its free too!...
    • Gary Starkman: I wonder if there are gonna be any new posts, I'm a big fan of posts like this one, really is helpful....
    • Raviraj: I am a big fan of woocommerce.. I have installed in many of my websites.. Glad to see these awesome free woocommerce ext...
    • Gary Starkman: Thanks for sharing, put a smile on my face....
    • vaibhav: I have a friend with one. This might help him. Il ask him to contact you...

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice