I am going to add limit and offset to this loop:
<?php
$images = get_field('am_c_gallery');
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div class="item-big"></div>
<?php endforeach; ?>
<?php endif; ?>
How can I add offset 1 and 5 image like this to my loop in output:
if first image: <div class="item-big"></div> if 2-5 images: <div class="item-small"></div> <div class="item-small"></div> <div class="item-small"></div> <div class="item-small"></div> if 5 - unlimited: <div class="item-hide"></div>
3
Answers
Use:
or
to know the 1st, 2nd to 5th element and to make condition with.
NB: to use foreach loop, make sure that the element of your array is not a key/value element otherwise the $index is not a number but the value of key
When faced with a mathematical process which must resolve in one of three ways, I like to leverage the three-way comparison operator as a fun outside-the-box approach.
Assuming an indexed array is being iterated, divide the indexes by 4 and round up — the result will be
-1
when$i
is0
,0
when$i
is1
,2
,3
, or4
, and1
when$i
is above4
.Using
printf()
andmatch()
are clean, maintainable functions to organize the processing and displaying. I’ll admit, I don’t know what data is in$image
, so that may need to be altered.Code: (Demo)