Adding a new row after every three images PHP

I’ve created an image gallery using Advanced Custom fields in WordPress. Now I need to add an if statement that will echo a new div and close it with the class of row after every three images. I have attempted to do it myself but it doesn’t work. Where am I going wrong?

<div class="container">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
        <?php endwhile; else: ?>
        <p>No Content</p>
    <?php endif; ?>

        $images = get_field('gallery_images');
        $counter = 3;

    <ul class="gal_list">
        <?php foreach( $images as $image ): ?>

            if($counter == 3){
                echo "<div class="row">";
                $counter = 0;

            <li class="col-md-4">
                <a class="img-responsive" href="<?php echo $image['url']; ?>" rel="<?php the_title() ?>"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>

                if($counter == 0){
                    echo "</div>";
        <?php endforeach; ?>

Read more here: Adding a new row after every three images PHP

Leave a Reply

Your email address will not be published. Required fields are marked *