Answer the question
In order to leave comments, you need to log in
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.
Answer the question
In order to leave comments, you need to log in
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 > 6
in 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>';
$i == 1;
while ( $query->have_posts() ) {
$query->the_post();
if ( $i === 6 ) {
# после шестого элемента добавляем какой-то контент
}
$i++;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question