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; ?>

    <?php 
        $images = get_field('gallery_images');
        $counter = 3;
    ?>

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

        <?php 
            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>
            </li>

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

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


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: