R
R
Roman Blinov2021-10-04 18:08:52
WordPress
Roman Blinov, 2021-10-04 18:08:52

How to display posts in WP in multiple containers?

I have a container which should have 6 entries. When there are 6 entries in it, the output of posts should stop and continue in another similar container. I know how to display records, but I don't know how to break the loop in one container and continue it in another.

615b18ee8e4da761305883.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael R., 2021-10-04
@plinetus

Output posts in a loop, as soon as the 7th iteration comes - create a new container.
You don't need to break the cycle.
An example of creating a new container, with the number of posts > 6in the current container.

echo '<div class="container">';

$i = 1;

while($query->have_posts()) {
  $query->the_post();

  if($i < 7) {
    // выводим пост
    echo '<div class="container__item">' . $post . '</div>';

  } else {
    // обнуляем счетчик
    $i = 0;
    
    // закрываем .container
    echo '</div>';

    // открываем новый .container
    echo '<div class="container">';

    // выводим пост
    echo '<div class="container__item">' . $post . '</div>';
  }

  $i++;
}

// закрываем .container
echo '</div>';

A
Artem Zolin, 2021-10-05
@artzolin

$i == 1;
while ( $query->have_posts() ) {
  $query->the_post();

  if ( $i === 6 ) {
    # после шестого элемента добавляем какой-то контент
  }

  $i++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question