Many people want to show the latest posts from there selected categories or all categories on home page.
I this article I will show how easily you can achieve this.
Please following code in your home page which is your theme’s index.php file.

<?php
$categories=get_categories($all_categories);
 foreach($categories as $category) {
 $post_args=array(
 'showposts' => 1, // you can fetch number of articles from each category
 'category__in' => array($category->term_id),
 'caller_get_posts'=>1
 );
 $posts=get_posts($post_args);
 if ($posts) {
 echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
 foreach($posts as $post) {
 setup_postdata($post); ?>
 <div>
 <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
 <?php the_title(); ?></a></p>
 <p><?php the_excerpt(); ?>
 </div>
 <?php
 } // foreach($posts
 } // if ($posts
 } // foreach($categories
?>

if you want to show the posts from specific articles then use following code.

<?php
$selected_categories=array(
 'include' => '1,4,9',
 'order' => 'ASC'
 );

$categories=get_categories($selected_categories);
 foreach($categories as $category) {
 $post_args=array(
 'showposts' => 1, // you can fetch number of articles from each category
 'category__in' => array($category->term_id),
 'caller_get_posts'=>1
 );
 $posts=get_posts($post_args);
 if ($posts) {
 echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
 foreach($posts as $post) {
 setup_postdata($post); ?>
 <div>
 <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
 <?php the_title(); ?></a></p>
 <p><?php the_excerpt(); ?>
 </div>
 <?php
 } // foreach($posts
 } // if ($posts
 } // foreach($categories
?>

Written by Purab Kharat

I Purab am from India, Pune. I am WordPress and Drupal Architect and consultant. I worked on many technologies like Ruby on Rails, Flex, PHP, jQuery etc... Senior Technical Lead at Smartsourcing Global Pvt Ltd in Princeton, NJ, USA.I contributed to many wordpress plugins and Drupal Modules. I contributed to Drupal core also.Connect to purab on LinkedIn or Follow @purab on Twitter or Follow me on Google+ or Write Mail to purabdk@gmail.com or Chat with me on Skype : purabdk

This article has 4 comments

  1. sam123 Reply

    Very helpful article. I have been looking for a way to do this all my life. Thanks for the codes.

  2. Kristof Reply

    What about if I choose certain categories but I don’t want one of the child-categories of one of these chosen categories?

    How do I exclude this category?

    I tried to add ‘exclude’ => ’35’, But that does not seem to work.

    Any thoughts? :-)

Leave a Comment

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>