Previous / Next Navigation Loop

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


Is this still the way to do this?

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.

Still works. I’ve just tried it. Thanks

1 Like

Code can be shortened to

<?php if ($prev = $page->prevListed() ?? $page->siblings()->listed()->last()): ?>
  <a href="<?= $prev->url() ?>">previous page</a>
<?php endif ?>

<?php if ($next = $page->nextListed() ?? $page->siblings()->listed()->first()): ?>
  <a href="<?= $next->url() ?>">next page</a>
<?php endif ?>

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

My collections is just a simple $site->find(‘projects’)->children()->listed() and then i filterBy:

$featured = $kirby->collection('projects')->filterBy('featured', 'true');

So my ->nextListed() looks like this:
<?php if ($next = $page->nextListed($featured) ?? $page->siblings()->first()) : ?>

When i var_dump my $featured it shows the correct List of pages:

[0]=> string(39) “projects/wohnuberbauung-geistlich-areal” [1]=> string(32) “projects/einfamilienhaus-pantloo” [2]=> string(34) “projects/okonomiegebaude-lutzelsee” [3]

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.