A
A
Alexey Chernov2016-04-24 21:25:03
CMS
Alexey Chernov, 2016-04-24 21:25:03

How to wrap every 4 posts in a div?

How to wrap every 4 posts with wp_query function in a div?
Give a more detailed answer.
Thank you)

<?php $args = array( 
                        'posts_per_page' => 5, 
                        'orderby' => 'date', 
                        'category_name' => 'team', 
                         );

                    $query = new WP_Query( $args );

                    // Цикл
                    if ( $query->have_posts() ) {
                        while ( $query->have_posts() ) {
                            $query->the_post();?>
                        <?php
                            $thumb_id = get_post_thumbnail_id();
                            $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
                        ?>
                        
  <div class="specialist col-6">
        <div class="sp-block-left">
          <div class="image-sp" style="background-image: url('<?php echo $thumb_url[0]; ?>');"></div>
        </div>
        <div class="sp-block-right">
          <a href="<?php the_permalink(); ?>"><h3><?php the_title();?></h3></a>
          <p class="despription"><?php the_tags(); ?></p>
          <p><?php the_excerpt(); ?></p>
        </div>
      </div>


                         <?php 
                              }
                            } 
                            wp_reset_postdata();?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WP Panda, 2016-04-25
@Limme

The task is trivial.
Add a counter, and modulo the value.

A
Alexander N++, 2016-04-25
@sanchezzzhak

via array_chunk($array,4);

$args = array( 
                        'posts_per_page' => 5, 
                        'orderby' => 'date', 
                        'category_name' => 'team', 
                         );
    $postsArr = get_posts( $args );
    $chunkPosts = array_chunk($postsArr ,4);
    foreach($chunkPosts as $posts){
        echo '<div class="див блока">';
             foreach($posts as $post){
                   // Выводим посты в блоке.
             }
        echo '</div>';
    }

Like so if I correctly understood VP.

D
Dmitry, 2021-04-07
@hitn89

<div class="wrapper">
foreach($chunkPosts as $key => $posts){
   echo ($key > 0 && $key % 4 == 0) ? '</div><div class="wrapper">' : '';
   // то, что нужно оборачивать
}
</div>

If it fits, then welcome to visit: hitn.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question