Get first two Images in snippet

Can someone help modify the Snippet to get two images instead of one? I’ve tried this (with my limited knowledge of PHP) but it only works for the first iteration of the loop:

<li>
    <a href="<?= $project->url() ?>">
      <?php if($image = $project->images()->sortBy('sort', 'asc')->first()): $thumb = $image->crop(600, 600); ?>
             <img src="<?= $thumb->url() ?>" alt="Thumbnail for <?= $project->title()->html() ?>" />
      <?php endif ?>
      <?php if($image = $project->images()->sortBy('sort', 'asc')->nth(2)): $thumb = $image->crop(600, 600); ?>
            <img src="<?= $thumb->url() ?>" alt="Thumbnail for <?= $project->title()->html() ?>" />
      <?php endif ?>
    </a>
</li>

I’m sure there is a better way to do this (passing the first two images to an array, for example) but as I mentioned, I’m currently limited by my knowledge of PHP syntax!
Thanks for any help!
Jamie

Excactly, you better fetch the first two images of a project first, then loop through the collection:

<?php
$images = $project->images()->sortBy('sort', 'desc')->limit(2);

foreach($images as $image): ?>
<li>
    <a href="<?= $project->url() ?>">
        <?php  $thumb = $image->crop(600, 600); ?>
        <img src="<?= $thumb->url() ?>" alt="Thumbnail for <?= $project->title()->html() ?>" />
    </a>
</li>
<?php endforeach ?>
1 Like

Thanks a lot! I removed the ‘endif’ and it works perfectly. Kirby is my first experience building a CMS and I love it so far!