Wrap every 2 WordPress posts inside Bootstrap Row Class

I need to know how to auto adding x numbers of WordPress posts inside .row class , cause i work on Bootsrap.

Thats my code for Posts loop.

                <div id="main" class="container" role="main">

                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <article class="col-md-6" id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?> role="article">

                        <header class="article-header">

                            <h1 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                            <p class="byline vcard"><?php
                                printf( __( 'Posted <time class="updated" datetime="%1$s" pubdate>%2$s</time> by <span class="author">%3$s</span> <span class="amp">&</span> filed under %4$s.', 'bonestheme' ), get_the_time('Y-m-j'), get_the_time(get_option('date_format')), bones_get_the_author_posts_link(), get_the_category_list(', '));
                            ?></p>

                        </header>

                        <section class="entry-content clearfix">
                            <?php the_content(); ?>
                        </section>

                        <footer class="article-footer">
                            <p class="tags"><?php the_tags( '<span class="tags-title">' . __( 'Tags:', 'bonestheme' ) . '</span> ', ', ', '' ); ?></p>

                        </footer>



                    </article>
                    <?php endwhile; ?>

Related posts

Leave a Reply

1 comment

  1. In the past, I’ve created a counter in PHP and placed it right before the end of the loop. The value of the counter will increment by one each time the loop runs. This means you can write a piece of code that says, if the counter is equal to a certain number (or, more plainly, after a certain number of blog posts), perform a certain action.

    Try this:

          <div id="main" class="container" role="main">
              <div class="row">
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
                      <article class="col-md-6" id="post-<?php the_ID(); ?>" role="article">
    
                      <!-- loop content -->
    
    
                      </article>
                    <?php $counter++;
                      if ($counter % 2 == 0) {
                      echo '</div><div class="row">';
                    }
                    endwhile; endif; ?>
              </div><!-- /row -->
        </div><!-- /container -->
    

    In this case, you’re saying that if the counter is divisible by two (which is what you’ll need if your posts have a class of col-md-6), echo in a closing div tag, and open a new row.