Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Punal Chotrani
Punal Chotrani
8,817 Points

How do is wrap every 2 posts inside a <div class='row'>?

Hi All,

How do i wrap a bootstrap <div class = row> after every 2 posts, using the while loop.

This is my current loop.

<!--Query Parameters--> <?php $page_id = (is_front_page() ? 0 :get_the_ID()); //Gets the id for the current page. $childpages = new WP_Query(array( 'post_type' => 'page', 'post_parent' => $page_id, 'posts_per_page' => -1, 'post__not_in' => array(get_option('page_on_front')), 'orderby' => 'menu_order date', 'order' => 'ASC' ) ); ?> <!--Query Parameters END--> <?php if ($childpages->have_posts()) { echo '<div class="row"><div class="row-height">'; } while ($childpages->have_posts()) : $childpages->the_post(); ?> <div class="col-md-6 col-sm-height"> <div class="inside inside-full-height"> <article> <div class="entry-header"> <h2> <a href="<?php echo make_path_relative(get_page_link()); ?>"> <?php the_title(); ?> </a> </h2> </div> <div class="entry-content clearfix"> <?php $image_id = get_post_thumbnail_id($page->ID); $image_url = wp_get_attachment_image_src($image_id, 'landing-page-children-thumb', false); if ($image_url) { ?> <a href="<?php echo make_path_relative(get_page_link($page->ID)) ?>" class="thumbnail" title="<?php echo $page->post_title ?>"> <img src="<?php echo(make_path_relative($image_url[0])); ?>" class="img-responsive"> </a> <?php } ?> </div> </article> <?php $i = 1; if ( $i % 2 === 0 ) { echo '</div></div><div class="row"><div class="row-height">'; } ?> </div> <?php $i++; endwhile; wp_reset_postdata(); echo '</div></div>'; ?>

All my post get looped how i want it. But currently the layout is not working, and wondering if i'm adding the while loop in the right place, or maybe some parameter is missing.

Thanks.