Home › Forums › ACF PRO › Next/Previous ID’s of Gallery Images? › Reply To: Next/Previous ID’s of Gallery Images?
When you run your foreach loop, you can also define an index variable. And you can use that index variable to get the prev and next.
Something like:
<?php
$project_gallery_thumbnails_images = get_field( 'project_gallery_thumbnails' );
// let store a total count for later use
$project_gallery_thumbnails_count = count($project_gallery_thumbnails_images);
?>
<?php if ( $project_gallery_thumbnails_images ) : ?>
<?php foreach ( $project_gallery_thumbnails_images as $index => $project_gallery_thumbnails_image ): ?>
<a class="thumbnail-link" href="#image_<?php echo $project_gallery_thumbnails_image['id']; ?>">
<img src="<?php echo $project_gallery_thumbnails_image['sizes']['thumbnail']; ?>" alt="<?php echo $project_gallery_thumbnails_image['alt']; ?>" />
</a>
<div id="image_<?php echo $project_gallery_thumbnails_image['id']; ?>" class="lb-overlay" >
<?php if ($index != 0): // dont do it on the first one ?>
<a href="#image_<?php echo $project_gallery_thumbnails_images[$index - 1]['id']; ?>">Previous Gallery Image ID</a>
<?php endif; ?>
<a class="thumbnail-link-close" href="#page">
<img class="lazyload" src="<?php echo $project_gallery_thumbnails_image['url']; ?>" alt="<?php echo $project_gallery_thumbnails_image['alt']; ?>" />
</a>
<?php if ($index != $project_gallery_thumbnails_count - 1): // dont do it on the last one ?>
<a href="#image_<?php echo $project_gallery_thumbnails_images[$index + 1]['id']; ?>">Next Gallery Image ID</a>
<?php endif; ?>
</div> <!--lb-overlay-->
<?php endforeach; ?>
<?php endif; ?>
Cheers.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.