PHP count loop adding an extra line

I am trying to wrap every 3 articles in a div so that my HTML looks like this:

<div class="row">
  <article>Article One</article>
  <article>Article Two</article>
  <article>Article Three</article>
</div>
<div class="row">
  <article>Article Four</article>
  <article>Article Five</article>
  <article>Article Six</article>
</div>

Below is my PHP. This is what I currently have, however an extra line is added at the beginning, which I don't want.

$i = 0;
echo '<div class="row">';
if ($my_query->have_posts()) : 
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if($i % 3 == 0) {echo '</div><div class="row">';}?>

The above means the following:

<div class="row"></div> //I don't want this to be in the HTML
<div class="row">
  <article>Article One</article>
  <article>Article Two</article>
  <article>Article Three</article>
</div>
<div class="row">
  <article>Article Four</article>
  <article>Article Five</article>
  <article>Article Six</article>
</div>

I tried changing $i = 0to $i = 1, but that didn't work either. This prints this in the markup:

    <div class="row">
      <article>Article One</article>
      <article>Article Two</article>
    </div>
    <div class="row">
      <article>Article Three</article>
      <article>Article Four</article>
      <article>Article Five</article>
    </div>
    <div class="row">
      <article>Article Six</article>
    </div>

I tried different combinations of numbers, but I could not get it to simply wrap every 3 articles.

+4
source share
3 answers

Write your condition as shown below: -

if(!empty($i) && $i % 3 == 0)
+3
source

HTML , . :

$listItems = array();
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) {
        $my_query->the_post();

        // Convert the post to <article>...</article>
        // and put the result in $articleHtml

        $listItems[] = $articleHtml;

        if (count($listItems) === 3) {
            // We have 3 items - print them
            echo '<div class="row">' . implode('', $listItems) . '</div>';
            $listItems = array();
        }
    }
}

// Don't forget the last items
if (count($listItems)) {
    echo '<div class="row">' . implode('', $listItems) . '</div>';
}
+2

.

<div>, <div> open </div> . " , ".

. , , .

:

// $i always count how many articles were already displayed
// none yet
$i = 0;
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) {
        if ($i % 3 == 0) {
            // $i is multiple of three
            // Zero or more complete groups were already displayed
            // Start a new group
            echo '<div class="row">';
        }

        // Display the current article and count it
        $my_query->the_post();
        $i ++;

        // After each group of three, close the group
        if ($i % 3 == 0) {
            // A multiple of three articles was displayed; close the group
            echo '</div>';
        }
    }

    // If the last group is not complete then must close it now
    if ($i % 3 != 0) {
        // The last group contains 1 or 2 articles; it was not closed yet
        echo '</div>';
    }
}
0

Source: https://habr.com/ru/post/1681885/


All Articles