E
E
Eugene Chefranov2018-04-17 21:40:30
PHP
Eugene Chefranov, 2018-04-17 21:40:30

How to wrap a div every 3 entries?

There is the following record output

<?php
$query = new WP_Query('cat=9'); 
if( $query->have_posts() ){
  while( $query->have_posts() ){ $query->the_post();
  ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>

  <?php
  }
  wp_reset_postdata(); 
} 
else echo 'Записей нет.';
?>

It is necessary to wrap every three records in one div (container). How to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vorotnev, 2018-04-17
@Chefranov

PHP has Modulo and WP_Query has $current_post:

if ( $query->current_post % 3 === 0 ) {
    ... // тут див
}

From memory, $current_post seems to start at 0, so it's possible:
$current = $query->current_post + 1;
if ( $current % 3 === 0 ) {
    ... // тут див
}

S
Sergey, 2018-04-17
@gangstarcj

It's easy to do it.
Learn what a cycle is. Find out what a variable is an integer. Go to class 1 to learn how to add 1 + 1 to get 3. Go to second class to learn how to divide any number by 3. and then figure it out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question