I have 2 columns blog and I want to wrap every 2 posts using row class, so i try:

<?php $counter = -1; ?>
<?php while ( have_posts() ) : the_post();?>
    <?php $counter++;?>
    <?php if ($counter % 2 == 0) : ?>
        <?php echo '<div class="row">'; ?>
    <?php endif; ?>
        <?php get_template_part( 'template-parts/two-columns', get_post_format() );?>
    <?php
    if ($counter % 2 != 0) : ?>
        <?php echo '</div>'; ?>
    <?php endif; ?>
<?php endwhile; ?>

but if I have 2n+1 posts on page I can’t close div – this part of code doesn’t work

if ($counter % 2 != 0) : ?>
    <?php echo '</div>'; ?>
<?php endif; ?>

How to solve this problem?

Read more here: WordPress – Wrap every 2 posts


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: