Inserting HTML to close and open divs in WP_Query loops

You could simplify things by using the array_chunk() PHP function, to split an array into smaller chunks. Then you don’t need to worry about opening and closing divs with some math tricks.

Let’s rewrite your code snippet and hopefully make it easier to work with:

// Let's get all posts with thumbnail set in some category
$args = [ 
    'cat'            => 22,
    'posts_per_page' => 9,
    'meta_key'       => '_thumbnail_id'
];

// Fetch posts
$query = new WP_Query( $args );

if( $query->have_posts() )
{
    while ( $query->have_posts() )
    {
        $query->the_post(); 

        // Collect all items into a temp array    
        $tmp[] = sprintf( 
            '<div class="small-1 large-4 columns"><a href="https://wordpress.stackexchange.com/questions/210604/%s">%s</a></div>',
            get_permalink(),
            get_the_post_thumbnail( get_the_ID(), 'large' )
        );
    } 

    // Split the divs into rows of 3 items
    $rows = array_chunk( $tmp, 3 );

    // Housecleaning
    unset( $tmp );
    wp_reset_postdata();

    // Output the rows
    foreach( $rows as $row )
        printf( '<div class="row">%s</div>', join( '', $row ) ); 

}

Look Ma, no math used here! 😉

Hopefully you can adjust this to your needs.

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet giriÅŸpinbahis giriÅŸmakrobet giriÅŸpulibet giriÅŸmobilbahis giriÅŸkolaybet giriÅŸ