Support

Account

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