• Hello,

    i need to loop all categories with latest 5 posts in index page.

    Preview: https://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)
  • 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

    (@modeman)

    So, now i have this loop code: Loop code

    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

    (@modeman)

    Anybody?

    Thread Starter modeman

    (@modeman)

    Up

    Thread Starter modeman

    (@modeman)

    Up

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Loop all categories with posts’ is closed to new replies.