<?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!