Loop foreach 'reloop' the first 2 pages

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

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

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

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.

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)

1 Like

Yes, this seems to work :slight_smile: