H
H
hypero2018-06-24 10:33:32
WordPress
hypero, 2018-06-24 10:33:32

Display articles of each Wordpress category?

Hello.
How can you display articles of different categories in this way: for example, there are 4 containers, in these containers you need to place 5 articles in each block of different categories. For example, in one container there will be 5 articles of one category, in another container 5 articles of another category, etc.
5b2f48d46bca9501165250.png
There is this code:

<article class="service-content__block">
      <div class="service-content__wrap">
            <img src="images/img.png" alt="" class="service-content__image">
      </div>
      <div class="service-content__right">
            <h3 class="service-content__title"><?php echo get_cat_name(9);?></h3>
            <?php
              $id=9;
              $n=6;
  $recent = new WP_Query("cat=$id&showposts=$n"); 
  while($recent->have_posts()) : $recent->the_post();
  ?>
  <a class="service-content__link" href="<?php the_permalink() ?>" rel="bookmark">
  <?php the_title(); ?>
  </a>
<?php endwhile; ?>
      </div>
</article>
<article class="service-content__block">
      <div class="service-content__wrap">
            <img src="images/img.png" alt="" class="service-content__image">
      </div>
      <div class="service-content__right">
            <h3 class="service-content__title"><?php echo get_cat_name(10);?></h3>
            <?php
              $id=10;
              $n=6;
  $recent = new WP_Query("cat=$id&showposts=$n"); 
  while($recent->have_posts()) : $recent->the_post();
  ?>
  <a class="service-content__link" href="<?php the_permalink() ?>" rel="bookmark">
  <?php the_title(); ?>
  </a>
<?php endwhile; ?>
      </div>
</article>

It displays only articles of one category with a certain category id, and you have to insert such a section many times, I would also like it to be displayed in a cycle.
Thanks for your help in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Plotnikov, 2018-06-24
@TrueDevs

Get all categories into an array, then foreach iterate through the array
Like this...

$cats = get_categories();  // или только нужные категории

foreach ( $cats as $cat){
WPQuery .... $cat->ID
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question