A
A
Andrey2021-07-05 17:04:31
PHP
Andrey, 2021-07-05 17:04:31

How to separate blocks in a wordpress loop?

Help to separate the output of records in a cycle ; I display
the news record type;
now the cycle looks like this

<div class="news">
     <div class="item">
          <a href="news1">Новость1</a>
     </div>
     <div class="item">
          <a href="news2">Новость2</a>
     </div>
     <div class="item">
          <a href="news3">Новость3</a>
     </div>
     <div class="item">
          <a href="news4">Новость4</a>
     </div>
</div>


but the loop output is needed as a result of this kind
<div class="news">
     <div class="item">
          <a href="news1">Новость1</a>
          <a href="news2">Новость2</a>
     </div>
     <div class="item">
          <a href="news3">Новость3</a>
          <a href="news3">Новость4</a>
     </div>
</div>

and so on, i.e. 2 materials in 1 iteme

here is the code

<div class="news">
        <?php
                    // Запрашиваем продукты
                    $query = new WP_Query( [
                        'post_type'      => 'news',
                        'posts_per_page' => 6,
                        'paged'          => get_query_var( 'page' ),
                    ] );
                    // Обрабатываем полученные в запросе продукты, если они есть
                    if ( $query->have_posts() ) {

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

<div class="item">
<a href="<?php the_permalink() ?>"><?php the_title(); ?>
</div>



 <?php }
                        wp_reset_postdata();
                        }
                        ?> 

</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Constantine, 2021-07-05
@psycho-coder

Through the remainder of the division
Rough example

foreach (range(1, 4) as $i => $item) {
    if ($i > 0 && $i % 2 == 0) {
        echo '</div><div class="item">';
    }
    echo '<a href="news' . $item . '">Новость' . $item . '</a>';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question