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.
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.