How to Display Recent Posts From a Specific Category Using Widgets

One of the things I like most about WordPress is the ability to pretty much do anything you want with it. With the help of a plugin, or custom PHP code, you can accomplish most things. If you would like to display a list of recent posts from a specific category in your sidebar, follow the steps below:

Step #1
Download the PHP Code Widget created by Otto.

Step #2
Upload the plugin to the wp-content/plugins directory on your server and activate it from within the WordPress dashboard.

Step #3
Go to Appearance > Widgets screen in your WordPress dashboard and choose the PHP Code widget and place it into the appropriate widget area you wish to display the list.

Step #4
Copy the code below and paste it into the text box for the PHP Code widget.

<ul> <?php $recent = new WP_Query("cat=3&showposts=5"); while($recent->have_posts()) : $recent->the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>

Notes
You can specify the category you wish to display in the sidebar by replacing the cat=3 with the category ID you want to display. If you want to add/reduce the number of posts shown, simply change the showposts=5 to any number. The plugin/code has been tested with WordPress 2.8 and was successful.

Screenshot
PHP Code Text Widget

About Brian Gardner
I am a hopeless Starbucks addict, freelance web designer/internet consultant living in Chicago. I write poetry and also love to ski. Lastly, I am the CEO/founder of StudioPress, which develops WordPress themes for business and individuals. Follow me @bgardner.

Comments

  1. Steve Adams says:

    This is great stuff! Very helpful and opens up more choices and flexibility in design.

    Thanks for the helpful tip Brian!
    Steve

  2. Mijk says:

    Thanks, but I think, there is way better solution for everyone – Justin Tadlock’s Query Posts Pluginhttp://justintadlock.com/archives/2009/03/15/query-posts-widget-wordpress-plugin

    In widget settings you just use GUI to select category, number of posts and other query parameters and then you instantly go.

    :) Good Work!

  3. Mijk says:
  4. violet says:

    I like the Category Posts Widget:

    http://wordpress.org/extend/plugins/category-posts/

    Why do all this coding if there’s already a great widget that accomplishes the same thing easily? :)

Speak Your Mind

*