# Get first two Images in snippet

**URL:** <https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343>\
**Category:** Questions\
**Tags:** v2\
**Created:** [May 4, 2018, 11:10am UTC](https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343 "2018-05-04T11:10:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [May 4, 2018, 11:10am UTC](https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343/1 "2018-05-04T11:10:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 4, 2018, 11:13am UTC](https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343/2 "2018-05-04T11:13:51Z")

</div>

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

```auto
<?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 ?>

```

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [May 4, 2018, 11:28am UTC](https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343/3 "2018-05-04T11:28:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:28pm UTC](https://forum.getkirby.com/t/get-first-two-images-in-snippet/10343/4 "2025-02-06T17:28:16Z")

</div>


