title: Php foreach loop wrapping every 2 items
body: <pre><code>&lt;div class="puffar"&gt;
          &lt;?php
          //Set up the objects needed
          $my_wp_query = new WP_Query();
          $all_wp_pages = $my_wp_query-&gt;query(array('post_type' =&gt; 'page'));
  
          //Get children
          $children = ($post-&gt;post_parent) ? get_page_children( $post-&gt;post_parent, $all_wp_pages ) :  get_page_children( $post-&gt;ID, $all_wp_pages );
  
          $i = 0;
          //Build custom items 
          foreach($children as $child){ 
          $i++;
  
          /*
          if (i % 2 == 0) { ?&gt;
  
          &lt;?php
          } */
          ?&gt;
  
  &lt;div class="col-sm-6"&gt;
      &lt;div class="puff"&gt;
          &lt;div class="puff-image-holder"&gt;
              &lt;?php echo get_the_post_thumbnail( $child-&gt;ID, 'full' ); ?&gt;
          &lt;/div&gt;
          &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;?php echo get_the_title($child-&gt;ID); ?&gt;&lt;/legend&gt;
          &lt;/fieldset&gt;
          &lt;?php echo get_field("puff_introtext", $child-&gt;ID); ?&gt;
          &lt;?php
          $values = get_field( 'puff_lanktext', $child-&gt;ID );
          if (get_field( "popup_eller_lank", $child-&gt;ID ) == "popup") {
          ?&gt;
          &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;a class ="linktopage open-popup" href="&lt;?php echo get_page_link($child-&gt;ID); ?&gt;"&gt;&lt;?php echo get_field( "puff_lanktext", $child-&gt;ID ); ?&gt; &lt;/a&gt;&lt;/legend&gt;
          &lt;/fieldset&gt;
          &lt;?php
          } elseif (get_field( "popup_eller_lank", $child-&gt;ID ) == "extern") {
          ?&gt;
              &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;a class ="linktopage" href="&lt;?php echo get_field( "puff_lank", $child-&gt;ID ); ?&gt;"&gt;&lt;?php echo get_field( "puff_lanktext", $child-&gt;ID ); ?&gt; &lt;/a&gt;&lt;/legend&gt;
          &lt;?php
          } else { }
          ?&gt;
  
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;?php } ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Hi stackers!</p>
  
  <p>I need some php help on how to wrap the looped elements. I want to wrap 2 elements in a <code>&lt;div class="row"&gt;.</code> So basically <code>&lt;row&gt; &lt;item&gt; &lt;item&gt; &lt;/row&gt;</code></p>
  
  <p>I have tried with some modulo as you can see, some if statements is still there. I set i as 0, and was trying to put <code>&lt;div class="row"&gt;</code> when 1 % 2 = 0 but found no solution on how to close the tags correctly ( should be closed after the second item)
  Any chance you could help out me as a novice php hacker?</p>
  
  <p>EDIT:</p>
  
  <pre><code>    &lt;div class="puffar"&gt;
          &lt;?php
          //Set up the objects needed
          $my_wp_query = new WP_Query();
          $all_wp_pages = $my_wp_query-&gt;query(array('post_type' =&gt; 'page'));
  
          //Get children
          $children = ($post-&gt;post_parent) ? get_page_children( $post-&gt;post_parent, $all_wp_pages ) :  get_page_children( $post-&gt;ID, $all_wp_pages );
  
          $i = 0;
          //Build custom items 
          echo "&lt;div class='row'&gt;";
          foreach($children as $child){   
          ?&gt;
  
  &lt;div class="col-sm-6"&gt;
      &lt;div class="puff"&gt;
          &lt;div class="puff-image-holder"&gt;
              &lt;?php echo get_the_post_thumbnail( $child-&gt;ID, 'full' ); ?&gt;
          &lt;/div&gt;
          &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;?php echo get_the_title($child-&gt;ID); ?&gt;&lt;/legend&gt;
          &lt;/fieldset&gt;
          &lt;?php echo get_field("puff_introtext", $child-&gt;ID); ?&gt;
          &lt;?php
          $values = get_field( 'puff_lanktext', $child-&gt;ID );
          if (get_field( "popup_eller_lank", $child-&gt;ID ) == "popup") {
          ?&gt;
          &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;a class ="linktopage open-popup" href="&lt;?php echo get_page_link($child-&gt;ID); ?&gt;"&gt;&lt;?php echo get_field( "puff_lanktext", $child-&gt;ID ); ?&gt; &lt;/a&gt;&lt;/legend&gt;
          &lt;/fieldset&gt;
          &lt;?php
          } elseif (get_field( "popup_eller_lank", $child-&gt;ID ) == "extern") {
          ?&gt;
              &lt;fieldset class="linedHeadline hlmedium"&gt;
              &lt;legend&gt;&lt;a class ="linktopage" href="&lt;?php echo get_field( "puff_lank", $child-&gt;ID ); ?&gt;"&gt;&lt;?php echo get_field( "puff_lanktext", $child-&gt;ID ); ?&gt; &lt;/a&gt;&lt;/legend&gt;
          &lt;?php
          $i++;  
          if ($i % 2 == 0) { 
              echo "&lt;/div&gt;&lt;div class='row'&gt;";
          }
          } else { }
          ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;?php } ?&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This only wraps all my looped items, I want the div class=row to only wrap every 2 items</p>