Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a widget available that will list the latest x categories according to posting date, essentially providing a list of the most recently active topics?

share|improve this question
    
What if the latest post has two or more categories assigned to it and you wanted to show the latest 5 categories? –  Joshua Abenazer Dec 6 '11 at 3:22
    
I guess I'd just have to add a check on $cat_array length in the snippet from krembo99 and stop when it gets to length 5. –  rutherford Dec 6 '11 at 3:28

1 Answer 1

    <?php
        $cat_array = array();
        $args=array(
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 20,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post();
            $cat_args=array('orderby' => 'none');
            $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
            foreach($cats as $cat) {
              $cat_array[$cat->term_id] = $cat->term_id;
            }
          endwhile;
        }
        if ($cat_array) {
          foreach($cat_array as $cat) {
            $category = get_term_by('ID',$cat, 'category');
            echo '<a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'.'<br />';
          }
        }
        wp_reset_query();
    ?>
share|improve this answer
    
just to confirm that code gets the categories of the last 20 posts and displays a link to the category page? –  rutherford Dec 6 '11 at 3:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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