WordPress Tricks: How to Show Most Recent Posts from Specific Categories with Thumbnails

Here is the code I use to show a certain number of the most recent posts including their thumbnails from specific categories in a WordPress website.

To show the 3 most recent posts from Category 1, you would add this code to the appropriate template file of your site:

<?php
//for category 1, show 3 posts
$cat_args=array(
  'include' => '1',
  '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 class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

You will want to edit the category ID(s) in this code of course so that you call the posts from the correct categories of your site, and you may also want to edit the number of posts shown from the categories as well.

To show the 3 most recent posts from Categories 1, 2, AND 3, you would use this code:

<?php
//for categories 1, 2, and 3, show 3 posts
$cat_args=array(
  'include' => '1,2,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 class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

If you want to include the excerpt of each post, you would use this code:

 

<?php
//for category 1, show 3 posts
$cat_args=array(
  'include' => '1',
  '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 class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>
<br/><span class="excerpt"><?php the_excerpt(); ?></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

Let me know if you have any questions or need any further modifications to this code 🙂