search
top

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 style="margin-left:20px;">
<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.

Related Posts:

No Responses to “WordPress Tricks: Display a list of the titles of the most recent blog posts from several different categories”

Trackbacks/Pingbacks

  1. Get the titles of recent blog posts from a certain category using wp_query « Icode4you – PHP, CSS, HTML, and Javascript coding tips, tricks, help, and more - [...] can also check out my post http://icode4you.net/wordpress-tricks-display-a-list-of-the-titles-of-your-most-recent-blog-posts-fr... if you are looking for a way to get the most recent posts from …

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

top