I was looking to find a simple way to loop sub page navigation. I found a post for Kirby 2 by @jenstornell, in which he solves the problem with simple logic (thanks man - ridiculously helpful) - you can see the original post here.
I thought I would share the exact way I implemented the idea here, in case it helps anyone else with their templating:
<?php if ($page->hasPrevListed()): ?>
<!-- This will link to the previous published page -->
<a href="<?= $page->prevListed()->url() ?>">previous page</a>
<?php else: ?>
<!-- This will link to the last published page if you're currently viewing the first -->
<a href="<?= $page->siblings()->listed()->last()->url() ?>">previous page</a>
<?php endif ?>
<?php if ($page->hasNextListed()): ?>
<!-- This will link to the next published page -->
<a href="<?= $page->nextListed()->url() ?>">next page</a>
<?php else: ?>
<!-- This will link to the first published page if you're currently viewing the last -->
<a href="<?= $page->siblings()->listed()->first()->url() ?>">next page</a>
<?php endif ?>
Iām sure there are better ways to do this, but Iām just getting my head wrapped around this stuff, and it worked, which is a big win for me. I left the actual visible text inside the links the same as a design choice, as it results in a transparent loop for the user (I donāt need them to know theyāre on the first or last sub page).
I assume thereās a better way - Iām almost always using something a little hacky or ālow-techā that I find better solutions for in the future.
That being said, I also assume it will still work, though it might be worth pinging @texnixe about, in case thereās a better / more official pagination enhancement that Iām not aware of.
In my very similar cas, i want to have the nextListed from a collection of featured pages. But it always skips the first page. I must miss something, and canāt figured out what .
My collections is just a simple $site->find(āprojectsā)->children()->listed() and then i filterBy:
What do i miss? Why does it not take the first (wohnuberbauung-geistlich-areal) but allways the second (/einfamilienhaus-pantloo) in the $featured array? I also try to SortBy ānumā, āascā first, but didnāt do the trick.
Using $page->siblings()->first() doesnāt make sense if you use a collection as argument. You need to use $featured->first()/$featured->last() instead.
Thank you for responding quickly and clarifying things for me. I also had to change my $page variable with an if statement, as it was on the wrong level. Now everything is working perfectly! Much happiness over here.