Home » Php » How to remove unused div in wordpress loop?

How to remove unused div in wordpress loop?

Posted by: admin October 18, 2018 Leave a comment

Questions:

I am trying to make WordPress loop with php modulus. The scenario is wrapping every 3 posts in a div. My script below is working perfectly, except I found an unused empty div at the last (from inspect element).

$loop_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 6
);

$loop = new WP_Query( $loop_args );

<?php if( $loop->have_posts() ): ?>

<div class="row">
    <?php $count = 0; ?>
    <div class="col-md-6">
    <?php while( $loop->have_posts() ) : $loop->the_post(); $count++; ?>
        <?php if( $count % 3 == 0 ): ?>
        <article class="post-content"><?php the_title();?></article>
    </div> <!-- .col md 6 -->
    <div class="col-md-6">
    <?php else: ?>
        <article class="post-content"><?php the_title();?></article>
        <?php endif; // end modulus ?>
        <?php endwhile; // end while loop ?>
    </div> <!-- .col md 6 -->
</div> <!-- .row -->

<?php endif; wp_reset_postdata(); ?>

Please take a look at the last highlighted div in my attachement:

screenshot preview

How can I remove that unused empty div?

Thanks in advance.

Answers: