Home › Forums › General Issues › Query the same loop in multiple columns › Reply To: Query the same loop in multiple columns
I get what you’re trying to do, and I’ll post the code below with how to do it using a flat array but it’s unclear what the_query
is or how it relates to Advanced Custom Fields. Until the actual code you’re using is posted the below may be a good starting point for you:
<?php
$image_array = query(); // Whatever you're using to query the images
// Ensure have an image array to work with
if( ! empty( $image_array ) ) {
$per_block = 4;
$total_items = count( $image_array );
foreach( $image_array as $index => $image_id ) {
$nice_index = $index + 1; // Arrays start at 0 but count and per block start at 1. Calculate once.
$start = ( 0 == $index % 4 ); // Calculate if we're at the start
$end = ( $nice_index == $per_block || $nice_index == $total_items ); // Check if we've hit the end
// Display Opening Tag
if( $start ) {
print( '<div class="col-md-3">' );
}
// Display image by ID
wp_get_attachment_image( $image_id, 'full' );
// Display Closing Tag
if( $end ) {
print( '</div>' );
}
} // END Foreach
} // END Conditional
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.