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")))
{
$nextLinkTitle=$page->children($kirby->collection("guides-content"))->first()->title();
$nextLinkUrl=$page->children($kirby->collection("guides-content"))->first()->url();
}
elseif ($page->hasNext())
{
$nextLinkTitle=$page->next($kirby->collection("guides-content"))->title();
$nextLinkUrl=$page->next($kirby->collection("guides-content"))->url();
}
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:
<?php
$collection = $kirby->collection("some collection");
if ($next = $page->next($collection)) {
// do stuff
echo $next->url();
}
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())
{
$nextLinkTitle=$page->children($collection)->first()->title();
$nextLinkUrl=$page->children($collection)->first()->url();
}
if ($next = $page->next($collection))
{
$nextLinkTitle=$page->next($collection)->title();
$nextLinkUrl=$page->next($collection)->url();
}