Title: Loop all categories  with posts
Last modified: August 24, 2016

---

# Loop all categories with posts

 *  [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/)
 * Hello,
 * i need to loop all categories with latest 5 posts in index page.
 * Preview: [http://i58.tinypic.com/ta6mu8.png](http://i58.tinypic.com/ta6mu8.png)
 * Code:
 *     ```
       <div class="page-category">
       <div class="cat-name">Kategorija</div>
       <div class="row nomargin">
       <div class="item col-md-6 nopadding">
       <div class="thumb">
       <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/post.jpg" class="img-responsive" /></a>
       <div class="meta">
       <div class="comments"><a href="#">78</a></div>
       <div class="gallery"></div>
       </div>
       </div>
       <div class="title">
       <a href="#"><h3>Manto Petruškevičiaus dūzgesė - žinomos ir nelabai</h3></a>
       </div>
       </div>
       <div class="item col-md-6 nopadding">
       <div class="thumb">
       <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/post.jpg" class="img-responsive" /></a>
       <div class="meta">
       <div class="comments"><a href="#">78</a></div>
       <div class="gallery"></div>
       </div>
       </div>
       <div class="title">
       <a href="#"><h3>Manto Petruškevičiaus dūzgesė - žinomos ir nelabai</h3></a>
       </div>
       </div>
       </div>
       <div class="row three-posts nomargin">
       <div class="item col-md-4 nopadding">
       <div class="thumb">
       <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/post.jpg" class="img-responsive" /></a>
       <div class="meta">
       <div class="comments"><a href="#">78</a></div>
       <div class="gallery"></div>
       </div>
       </div>
       <div class="title">
       <a href="#"><h3>Manto Petruškevičiaus dūzgesė - žinomos ir nelabai</h3></a>
       </div>
       </div>
       <div class="item col-md-4 nopadding">
       <div class="thumb">
       <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/post.jpg" class="img-responsive" /></a>
       <div class="meta">
       <div class="comments"><a href="#">78</a></div>
       <div class="gallery"></div>
       </div>
       </div>
       <div class="title">
       <a href="#"><h3>Manto Petruškevičiaus dūzgesė - žinomos ir nelabai</h3></a>
       </div>
       </div>
       <div class="item col-md-4 nopadding">
       <div class="thumb">
       <a href="#"><img src="<?php bloginfo('template_url'); ?>/images/post.jpg" class="img-responsive" /></a>
       <div class="meta">
       <div class="comments"><a href="#">78</a></div>
       <div class="gallery"></div>
       </div>
       </div>
       <div class="title">
       <a href="#"><h3>Manto Petruškevičiaus dūzgesė - žinomos ir nelabai</h3></a>
       </div>
       </div>
       </div>
       </div><!-- ./page-category -->
       ```
   
 * Maybe someone can help with right loop code?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Lucas Karpiuk](https://wordpress.org/support/users/karpstrucking/)
 * (@karpstrucking)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106353)
 * Use `get_categories()` to build an array of Category objects, then `foreach` 
   Category, build a `WP_Query` that outputs the 5 most recent posts.
 *     ```
       $categories = get_categories();
       foreach ( $categories as $category ) {
         $args = array(
           'cat' => $category->term_id,
           'posts_per_page' => 5
         );
         $query = new WP_Query( $query );
         if ( $query->have_posts() ) {
           while ( $query->have_posts() ) {
             $query->the_post();
             /* do stuff here */
           }
         }
       }
       ```
   
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106480)
 * So, now i have this loop code: [Loop code](http://pastebin.com/AHMJQyb3)
 * But it loops the same post. I need to exclude those 5 posts, because first two
   posts and other three posts are different styling. How to do that?
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [11 years ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106505)
 * Anybody?
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [11 years ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106530)
 * Up
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [11 years ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106553)
 * Up

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Loop all categories with posts’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [modeman](https://wordpress.org/support/users/modeman/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/loop-all-categories-with-posts/#post-6106553)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
