How to select first 5 Images of in a gallerry

Hey,

I have a gallery and I want to display the first five images separately.

gallery:
  type: files
  layout: cards
  image:
    cover: true
    info: "{{ file.dimensions }}"

I tried " isNth()" but but the displayed images do not match the position.

<?php $images =  $page->gallery()->toFiles();
  foreach($images as $image): ?>

  <?php if ($image->is($images->first())): ?>
    <img src="<?= $image->url() ?>" alt="">
  <?php endif ?>
## Shows the first image in the gallery

  
  <?php if ($image->isNth(2)): ?>
        <img src="<?= $image->url() ?>" alt="">
   <?php endif ?>
## Shows the THIRD image in the gallery

  
  <?php if ($image->isNth(3)): ?>
        <img src="<?= $image->url() ?>" alt="">
   <?php endif ?>
## Shows the FOURTH image in the gallery


  <?php if ($image->isNth(4)): ?>
        <img src="<?= $image->url() ?>" alt="">
   <?php endif ?>
 ## Shows the SIXTH image in the gallery


  <?php if ($image->isNth(5)): ?>
        <img src="<?= $image->url() ?>" alt="">
   <?php endif ?>
 ## Shows the ELEVENTH image in the gallery 

<?php endforeach ?>

Is there another opportunity?

THX

Programming languages generally start counting from zero rather than one, so for the second image, you need to use ->isNth(1) and so forth.

Going through each image separately is unnecessary

<?php $images =  $page->gallery()->toFiles()->limit(5);

Then do a loop

Heh… beat me to it… i was going off to find the Docs page for limit()… There is a working example on that page.

1 Like

OMG it’s so easy :see_no_evil:

Thank you so much!!!