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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.