Support

Account

Home Forums Add-ons Gallery Field Get the latest images from a gallery field?

Solved

Get the latest images from a gallery field?

  • On my site i’ve got the gallery field on a page and am displaying the whole gallery there. But on the home page I’d like to be able to show the latest 4 images from that gallery (since it’ll keep getting added to).
    Is this possible? I’ve seen posts to show the first image, but i’m looking for the latest images (assuming they’re added to the end of the gallery).

    Possible??

  • sure. you need to count the images of the gallery, get the difference to 4, and loop through gallery but echo only the last 4 (the others loop without echo)

    something like this:

    $images = get_field('gallery');
    $count= count($images);
    if ($count > 4){
    $i = 0 - $count + 4;
    } else { $i=0;}
    foreach( $images as $image ): 
    $i++;
    if ($i > 0){
    echo $image['url'];
    }
    endforeach;

    not sure if code is correct, but it should help to made a correct one

  • That’s great! Seems to work perfectly!

    Thank you!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get the latest images from a gallery field?’ is closed to new replies.