WordPress Tricks: Display a list of the titles of the most recent blog posts from several different categories

Here is the code you can use to display the titles of your most recent posts in specific categories on a WordPress page.

Copy this block of code:

<?php
//for categories 1,5,3, show 1 post
$cat_args=array(
  'include' => '1,5,3',
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => 3,
      'category__in' => array($category->term_id),
      'caller_get_posts'=> 1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p><h4>Latest <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.' Posts</a>: </h4></p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>
</div>

Now, simply paste that block of code into the page template where you want to display the list of post titles.

You can include or exclude posts from certain categories if you wish and can also control how many post titles to show from each category.

Let me know if you have any questions or trouble with this.

If you are looking for a way to display a certain number of blog post titles from just one category, you can use the code in my post Get the titles of recent blog posts from a certain category using wp_query.

Meet the author

My name is Libby Fisher and I am an experienced freelance web developer based in Seattle WA. I am passionate about developing websites that are both beautiful AND easily usable because I believe that the most effective websites are a perfect balance of aesthetic appeal and usability/intuitiveness.

1 Comment

Leave a response

*

'