Support

Account

Home Forums Add-ons Gallery Field Exclude images from gallery

Solving

Exclude images from gallery

  • Hello,
    I’m using ACF Gallery add-on to display galleries on my website, so far it worked perfectly.

    Now my client wants to display only a small preview of the gallery, only four images and then all the rest. In other words I need to split the gallery in two parts.

    Getting the first 4 images is not a problem, I’m just outputting each one of them using <?php echo $images[x]['url']; ?>. It’s not the most elegant solution, but it works.

    I know that there is no “offset” parameter to use (but please, consider it a feature request), so I was wandering if there’s a way to exclude the first 4 images from the array generated by <?php $myimages = get_field('image_gallery'); ?>

    I’m not a php expert, so there’s probably a better approach to this problem. Any suggestion would be much appreciate.

  • The best way to remove them is to just remove them from the beginning of the array

    
    $myimages = get_field('image_gallery');
    for ($i=0; $i<4; $i++) {
      // make sure there's still something in the array
      // I don't know if array_shift will cause an error on an empty array
      if (!empty($my_images)) {
        $remove = array_shift($myimages);
      }
    }
    print_r($myimages);
    
  • Also, you can do something similar to truncate the array to the first 4

    
    $myimages = get_field('image_gallery');
    while (count($myimages) > 4) {
        $remove = array_pop($myimages);
    }
    print_r($myimages);
    
  • John – great tips. I’d suggest adding either/both snippets to the documentation, as it seems like a pretty common use case; I was looking to do the same and wouldn’t have found it without some digging!

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

You must be logged in to reply to this topic.