WordPress and Bootstrap 3 auto generate row in categories

I’m using bootstrap 3 and I would like to group 3 post categories per row to get a proper grid system but I don’t know how to make a counter. Can anybody help me with this?

Here is my code:

Read More
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

     $args = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      );

      $attachments = get_posts( $args );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<div class="col-md-4">';
                echo '<a href="';
                echo the_permalink(); 
                echo '">';
               echo wp_get_attachment_image( $attachment->ID, 'full' );
                echo '</a>';
                echo '<h3 class="category-title"><a href="';
                echo the_permalink();
                echo '">';
                echo the_title();
                echo '</a></h3>';
               echo '</div>';


              }
         }

     endwhile; endif; ?>

I would like to have something like this

<div class="row">
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
</div>
<div class="row">
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
    <div class="col-md-4">Content</div>
</div>

Thank you very much for your help.

Related posts

Leave a Reply

1 comment

  1. Use PHP’s MOD to test for a remainder:

    $i = 0;
    if ( have_posts() ) : while ( have_posts() ) : the_post();    
        $i++; 
    if($i%3 == 1){echo '<div class="row">'; }
             $args = array(
               'post_type' => 'attachment',
               'numberposts' => -1,
               'post_status' => null,
               'post_parent' => $post->ID
              );
    
              $attachments = get_posts( $args );
                 if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                       echo '<div class="col-md-4">';
                        echo '<a href="';
                        echo the_permalink(); 
                        echo '">';
                       echo wp_get_attachment_image( $attachment->ID, 'full' );
                        echo '</a>';
                        echo '<h3 class="category-title"><a href="';
                        echo the_permalink();
                        echo '">';
                        echo the_title();
                        echo '</a></h3>';
                       echo '</div>';
    
    
                      }
                 }
        if($i%3 == 0){echo '</div>';}
             endwhile; endif;