Home › Forums › Add-ons › Gallery Field › Limit Image Gallery › Reply To: Limit Image Gallery
add a counter and break when the limit is reached, or use a for loop instead of a foreach loop.
<?php
// using counter
$images = get_field('gallery');
if ($images) {
$counter = 1;
?>
<ul>
<?php
foreach( $images as $image ) {
?>
<li>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</li>
<?php
$counter++;
if ($couner == 10) {
break;
}
}
?>
</ul>
<?php
}
?>
<?php
// using for loop, this is the one I'd prefer
$images = get_field('gallery');
if ($images) {
?>
<ul>
<?php
for($i=0; $i<count($images) && $i<10; $i++) {
$image = $images[$i];
?>
<li>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
</li>
<?php
}
?>
</ul>
<?php
}
?>
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!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 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.