Prevnext for all visible pages/children

hi there

been trying to make the prev/next go through for all visible pages without success.
it can go through all siblings, but id like to see parents too.
in the example below, the desired outcome would be to move through 02-featured_b to 02-archive to 01-archive_a. (so “next page” would go through all visible pages in order)


ive tried solutions based on

can anyone lend me a spark or a whiff of a direction? :smile:

thanks for your attention

I’m on mobile right now so cant type much, but looping through $pages->index()->data should give you a flat list of all pages in order. Filter before the index() to only return visible pages.

This is a solution I use in a project (if you don’t want to go through the complete index):

<nav class="switch-page">
<?php if($page->hasPrev()) : ?>
    <a class="prev" href="<?php echo $page->prev()->url() ?>"><span>back</span></a>
<?php elseif (($page->parent()->hasPrev()) && ($page->parent()->prev()->hasChildren())) : ?>
       <a class="prev" href="<?php echo $page->parent()->prev()->children()->last()->url() ?>"><span>back</span></a> 
<?php endif ?>
<?php if($page->hasNext()): ?> 
    <a class="next" href="<?php echo $page->next()->url() ?>"><span>next</span></a>
<?php elseif (($page->parent()->hasNext()) && ($page->parent()->next()->hasChildren())) : ?>
    <a class="next" href="<?php echo $page->parent()->next()->children()->first()->url() ?>"><span>next</span></a>
<?php  endif; ?>

oh hi tex! i actualy tried your code from another topic on the old forum.
it gives me an error when im on the very first parent Fatal error: Call to a member function children() on a non-object in /kirby/core/page.php on line 537

Oh, hi, I thought that looked familiar :wink:. I’ll look into it as soon as I can, think it is still running fine on the project’s pages.

Have you tried the code I posted above? The stuff in old forum is very old, so that might not be functional anymore. The above code works with the current Kirby version including the 2.1 beta.

yea, tried it again now (just in case)
the first parent returns that error, and only cycles through siblings, not all visibles.

Oh, I see, that probably a misunderstanding. Maybe there is no parent from where you are starting this. Maybe the best idea would be to create a new collection of all relevant pages and then cycle through them (don’t know if that keeps the order). Otherwise, you need to check if the page has a parent first …

In my example, I don’t want to cycle through the parent, because the parent has no content of its own and is just a container for the children.

I’m not sure if this would help, it’s a sort of universal next/previous plugin.

  • You can move backwards & forwards down & up through levels (parent/children/grandchildren).
  • You also can page through an article index and then through the actual details pages (articles in demo).
  • Or just the index (press in demo) skipping the detail pages (details pages are still findable with search in demo).
  • It also works with Tag index pages and search indexes.
  • Although it is hard to see in the demo due to being minified, it is also used in the header in rel links for next & previous.

On Github see: uniNextPrev Plugin

Although it works in Kirby2, I’m sure it could be written in a much
better way, I just needed it for a project and was in a hurry.

1 Like

uninextprev has moved to a new home