I need a loop/plugin/widget to display latest posts from the category the user is browsing and at the same time if he visits the home page, it should show posts from all categories (not a specific one)

Any Ideas ???


here is a quick one: (edit: I updated some forgotten punctuation).

Plugin Name: Recent posts per category loop 
Plugin URI: http://en.bainternet.info
Description: answer to http://wordpress.stackexchange.com/questions/16616/recent-posts-per-category-loop
Author: Bainternet
Version: 1.0
Author URI: http://en.bainternet.info

  // Initialization function
  function WP_Widget_display_widget_init() 
    new WP_Widget_display_widget();

  // Your Class
  class WP_Widget_display_widget extends WP_Widget 
    // Constructor
    function WP_Widget_display_widget() 
      $widget_ops = array('description' => __('recent-posts-per-category-loop', 'display_widget'));
      $this->WP_Widget('display_widget', __('Display_widget'), $widget_ops);

    // Display Widget
    function widget($args, $instance) {
        $title = attribute_escape($instance['title']);
        echo $before_widget.$before_title.$title.$after_title;
        echo $after_widget;

    // When Widget Control Form Is Posted
    function update($new_instance, $old_instance) 
        if (!isset($new_instance['submit'])) 
            return false;
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;

    /* Your custom code starts here */
    /* ---------------------------- */

    /* Your Function */
    function display_widget()

      /* Your Code ----------------- */ 

      function display_widget(){
    //only run on home or category
    $args['post_status'] = 'published';
    $args['posts_per_page'] = 5;
    if (is_front_page()||is_home()|| is_category())
        return '';
    if (is_category()){
        $args['cat'] = (int)get_query_var('cat');
    $po = new WP_Query( $args );
    $out = '<ul>';
    while ($po->have_posts()){
        $out .= '<li><a href="'. get_permalink($post->ID).'" title="'.get_the_title($post->ID).'">'.get_the_title($post->ID).'</a></li>';

    $out .= '</ul>';
    echo $out;


  add_action('widgets_init', 'WP_Widget_display_widget_init');
  • Well, Thank you very much for your interest, But I'm afraid that I know nothing about php and also I'm still new to WP and this is my first site with it. I pasted the code in functions.php but it gave me syntax error ! can you help me any more ? – Bialy May 7 '11 at 22:36
  • Copy this into a new PHP file in your plugins directory and activate like a normal plugin – Bainternet May 8 '11 at 6:40
  • @:Bainternet: why you didn't published this plugin in wordpress? it's very useful for all.so please go ahead and upload to wordpress.org – Ramkumar M Aug 5 '11 at 12:28

you may try this plugin if you are not knowing about code.. Latest Post from Each Category Plugin.it'll help you:)

  • "For WordPress 2.0 through 2.8+" ... have you personally tested it on the latest version of WP? – t31os Dec 13 '11 at 21:45
  • i used this plugin in wordpress 3.1.2 version and it's working fine. – Ramkumar M Dec 14 '11 at 4:49
  • Tested on 3.3.2 – Maxim Krizhanovsky Apr 29 '12 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.