I have the following set up below. I’m trying to wrap ever 2 divs in a “row”.
When its an even number it works great, but when there’s an odd number, I end with an open div and get html errors. Any ideas on how to better make sure there is a closing div would be appreciated
<?php
$args = array(
'post_type' => 'portfolio-project',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$query = query_posts($args);
?>
<?php $i=1; ?>
<?php while (have_posts()) : the_post(); ?>
<?php if($i==1 || $i%2==1) echo '<div class="row">' ;?>
<div class="col-sm-6">
<?php the_title();?>
</div>
<?php if($i%2==0) echo '</div>' ; ?>
<?php $i++; endwhile; wp_reset_query();?>
You have to put and end close tag if needed. I change the counting method to be more clear.
Give this a try
Try
in place of
Keep the count of number of div’s that are open
Change this
to
And Check if count==1 while closing the div