Excluding certain templates from the $page-next()/prev() functionality

Hi there, I’m wanting to use the $page->next() / prev() navigation functionality to allow users to move around within a course-like structure, but I want to exclude certain blueprints/templates from the navigation. I can’t find a field that excludes a blueprint from navigation and I’ve tried passing in a collection to $page->next() that should filter out certain pages by template, but it doesn’t appear to have any affect whatsoever - all pages are still available. Could someone suggest the best route to do this please?

Hey @jdrever, welcome to the forum!

I’d love to answer your question, but it would be helpful if you actually posted the code you tried and that doesn’t work as expected. Thanks!

Hey @texnixe thanks for your welcome! Loving Kirby so far!

I’ve tried this various ways, but here is one example. I have a collection guides-content.php which is intended to exclude a particular template:

return function ($site) {
    return $site->index()->listed()->filterBy('template','!=','guide-section-header');

and I have some code that uses the collection:

if ($page->hasChildren($kirby->collection("guides-content")))
  elseif ($page->hasNext())

I can loop through the collection & see it is correctly excluding the template. But the $page->next(…) code effectively ignores the collection, returning all pages without exclusion. Any suggestions very welcome!

Your first if statement doesn’t make sense at all, because hasChildren() doesn’t accept any parameters.

Are you trying to show a next page link on every single page?

Usually, prev() and next() refer to a siblings collection, not the whole site index. Are you trying to loop through all pages of the site?

This is how it would usually work:

$collection = $kirby->collection("some collection");
if ($next = $page->next($collection)) {
  // do  stuff
  echo $next->url();
1 Like

Hi @texnixe thanks for your reply - that helped me out! Yes the intention is to provide next child or sibling navigation through a large sub-section of the site (a course-like structure), but excluding certain templates (which are headings rather than pages in the course structure). In case it’s useful for anyone else, here’s the code I’ve ended up with, and which works fine for my use-case:

  $collection = $kirby->collection("guides-content");
  if ($next = $page->children($collection)->first())

  if ($next = $page->next($collection))