i am trying to wrap each 4 results inside a LI and repeat for every 4 items like
<li>
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
</li>
PHP so far…. the loop ive attempted is of course not working 🙂
if ( $query->have_posts() ) {
$opnews .= '<ul class="newsitems orbit-slides-container">';
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$opnews_item_data = get_post_meta( $post_id, 'opnews_item', true );
if ($i%4 == 1) {
$opnews .= '<li>';
}
$opnews .= '<div class="columns large-3 small-12 medium-3">';
$opnews .= '<div class="panel green opacity-change">';
$opnews .= '<h1>' . get_the_title() . '</h1>';
$opnews .= get_the_content_with_formatting();
$opnews .= '</div>';
$opnews .= '</div>';
if ($count%4 == 0) {
$opnews .= '</li>';
}
endwhile;
$opnews .= '</ul>';
wp_reset_postdata();
}
You are using
$i
and$count
, so pick only one.Then you have to increment it between your
<li>
to get it working.And finally, you should check, once you finished the loop, that the last
<li>
has been echoed or you will get some trouble (a missing</li>
)Output :
The modulus operator (
%
) divides the number and returns the remainder. So, your lineif ($i%4 == 1)
probably isn’t what you’re after, as if it’s every 4th row, you’ll want it with no remainder.The
$count%4 == 0
line also doesn’t make much sense to me, as you’re not incrementing the number. You’re also not incrementing$i
.Try the following:
It’s not working because you never change count. Count is always 0, so
$count % 4 == 0
is always true. Also, unless it’s somewhere else you haven’t definedi
.Use only
count
(or onlyi
).It looks like you’re mixing both
$i
and$count
. One of them you’re using the modulous operator and comparing if the remainder after division is 1, and the other you’re comparing if the remainder is 0. Neither of them seems to be incrementing (and$i
doesn’t look to be defined from the snippet you’ve provided).Choose one,
$count
, and compare it with 0 using the modulous and be sure to increment it within the loop: