How to: prev() for sub-pages

Hey there,

I have trouble getting the prev() page navigation to work on my website. I want to link back from a product page to it’s parent – which is also a sub-page of a overview page.
Does prev() not work for su-page or am I just not getting it? :sweat_smile:

Thank you – and let me know if you’d need more info,
Linus

$page->prev() always works in relation to the current page, i.e. within the siblings of the current page. Since parents or children are not siblings of the current page, they are not taken into account.

To cycle between pages of a given collection, you could use the getPrev() and getNext() custom methods from my pagemethods repo. They calculate the prev/next pages based on the collection you pass to the method as parameter.

Ok I see, thank you!

I simplycreated a page-methods folder with an index.php file and pasted your code snippet.
After that I created a link on the page where I want to add the “Previous” page with:

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

But now it only cycles through the subpages and doesn’t link back to the parent overview page. What need to be changed to achieve this?

I basically want to navigate back from “Sup mit dem Team” to the “teams” overview

Screenshot 2020-02-10 at 15.04.09

You have to use the new methods instead of the prev() and next() methods and pass the collection as a parameter.

Ah – perfect, thanks! :slight_smile: