Support

Account

Home Forums Add-ons Gallery Field Specify number of gallery images to display on the front end

Solved

Specify number of gallery images to display on the front end

  • <?php $preview_gallery_images = get_field( 'preview_gallery' ); ?>
    <?php if ( $preview_gallery_images ) :  ?>
    	<?php foreach ( $preview_gallery_images as $preview_gallery_image ): ?>
    		<a href="<?php echo $preview_gallery_image['url']; ?>">
    			<img src="<?php echo $preview_gallery_image['sizes']['thumbnail']; ?>" alt="<?php echo $preview_gallery_image['alt']; ?>" />
    		</a>
    	<?php endforeach; ?>
    <?php endif; ?>

    Hey everyone,

    In short, I’m attempting to show (for example) the first 3 gallery images to a user with a ‘guest’ role, whereas a user with a ‘subscriber’ role will get the entire gallery. I’ve got the conditional logic sorted out for the user roles, but just need to know how add this limitation to the above code. Is that possible?

    Thanks in advance.

  • I would set a limit variable, based on your condition. Where, if the limit is 0 (zero) that means unlimited. Then in your foreach loop, the you’ll need a counter

    
    $counter = 0;
    foreach ($preview_gallery_images as $preview_gallery_image) {
      // before showing an image
      if ($limit > 0 && $counter > $limit) {
        // do not continue showing images
        break;
      }
    
      // your code for showing image
    
      // after showing image, increment counter
      $counter++;
    }
    
  • Thanks, John – you’re a hero! I actually ended up using your solutions from Gallery – Random and Limiter (and removed the ‘shuffle’) – works perfectly!

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

The topic ‘Specify number of gallery images to display on the front end’ is closed to new replies.