Posted on 1 October, 2008 By Kim Woodbridge 87 Comments

WordPress – How to List Recent Posts from One Category

 
Share

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. You can also connect with my Facebook page or follow me on Twitter.



    87 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
    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 Enabled

    Recent Comments

      • Ari Herzog: Agreed on the updates/inbox snafu. I rarely see page updates and I know where they are! .-= Ari Herzog´s last blog .....
      • jayson: Kim, Love the themes. I am looking to update the theme on my site to something more appropriate. My site is all abou...
      • vered: "I wish more people would use this. I think it’s silly when I get updates for things occuring in Kansas and California –...
      • Dennis Edell @ Direct Sales Marketing: I was speaking generally, not specifically this month. how exactly do you work things? .-= Dennis Edell @ Direct Sales M...
      • Lucy Beer: Unfortunately I think next to no-one actually sees those "Updates". They're buried in a subsection of the inbox and I do...
      • Kim Woodbridge: Hi Marbella - Thanks! I had a great time....
      • Kim Woodbridge: Hi Julie - I'll probably try kayaking again next summer but I don't know if I will become much of a kayaker - I had a di...
      • Kim Woodbridge: Hi Vered - If you ever get the chance, you should go. It's like a different world - it's so quiet and peaceful....

    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.

    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