# Loop foreach 'reloop' the first 2 pages

**URL:** <https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 19, 2018, 10:00am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249 "2018-04-19T10:00:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eriks](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/eriks/32/5312_2.png) [@eriks](https://forum.getkirby.com/u/eriks)\
**Post date:** [April 19, 2018, 10:00am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249/1 "2018-04-19T10:00:00Z")

</div>

Hello,  
I’m making a website with infinite scrolling. When the user reaches the end, the scrollbar jumps to the top. In order to make it more natural, I have to ‘reloop’ the first 2 page-titles in the end.  
My code looks like this:

```
<?php $items = page('items')->children()->visible(); ?>
<?php foreach($items->sortBy('sort', 'desc') as $item): ?>
<?= $item->slug() ?> […]
<?php endforeach ?>

```

Thanks in advance

---

<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:** [April 19, 2018, 10:12am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249/2 "2018-04-19T10:12:20Z")

</div>

This would add the first two items to the end, you can then loop through the merge collection like before:

```auto
$items = page(‘items’)->children()->visible();
$firstTwoItems = $items->limit(2);
$allItems = new Pages(array($items, $firstTwoItems));

```

---

<div class="post-metadata">

**Author:** ![eriks](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/eriks/32/5312_2.png) [@eriks](https://forum.getkirby.com/u/eriks)\
**Post date:** [April 19, 2018, 10:43am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249/3 "2018-04-19T10:43:48Z")

</div>

Thank you for responding.

Is it still an foreach like:  
`<?php foreach($allItems->sortBy('sort', 'desc') as $item): ?>`  
?

I don’t see the first two pages, even in the developer tools.

---

<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:** [April 19, 2018, 10:45am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249/4 "2018-04-19T10:45:38Z")

</div>

Hm, maybe it recognizes the duplicates and throws them out; then you would have to loop through your original collection and then after that through $firstTwoItems (without the merge)

---

<div class="post-metadata">

**Author:** ![eriks](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/eriks/32/5312_2.png) [@eriks](https://forum.getkirby.com/u/eriks)\
**Post date:** [April 19, 2018, 10:48am UTC](https://forum.getkirby.com/t/loop-foreach-reloop-the-first-2-pages/10249/5 "2018-04-19T10:48:31Z")

</div>

Yes, this seems to work 🙂

---

<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/loop-foreach-reloop-the-first-2-pages/10249/6 "2025-02-06T17:28:23Z")

</div>

Archived on February 6, 2025, 5:28pm UTC


