Now I wonder if there is a way to do the same thing inside a collection of page, like tags, because if I click on a project after seeing it in a tag page, the next one is not the next in this filtered collection but the next child of global parent.
I wondered out of curiosity if there was a native simple way to remember the “real” next in collection that I miss or if I have to use session and complicated tricks to do it (in this case, I will not implement it)
You could use an URL parameter to pass the tag to the project page. Instead of using the standard page methods, you could create your own custom page methods https://getkirby.com/docs/developer-guide/objects/page, that use the information from the parameter to determine the next/prev pages (custom page methods will be available in the upcoming 2.3 release).
Edit: That would in fact work if you copy the code from the protected methods _prev() and _next() into your own page methods. Then you can pass a filtered collection to those methods like this.
Note that the above is just a rough outline to illustrate the idea using the native protected _prev() and _next() methods (by hacking the core to make them public for testing). Also, you would have to check if the referer exists etc.
Thank you for the follow up ! I will look at it but I think that unfortunately it’s waaaay beyond my developer skills. I will try to implement it one day tough. Thanks for the detailed example.
The above methods are intended to replace the standard prev/next methods like they are used in the prevnext.php snippet of the starterkit. Instead of just fetching the next page, you would pass the collection to the method so that not only the sort order but also the filtering is taken into consideration when trying to get the next page.
The thing is that you have to pass the information about the collection down to the single page, so the $collectionvariable would be something like this
Ah yes, I understand now what is happening here. In this case you can load $collection (or another variable) with the list you want to navigate through with ‘next’ and ‘previous’. But the right order should already be in $collection.
Thanks for helping out and writing the custom page method. This is something that can be used in so many situations.
and what I need is to navigate with a scrollTo effect to each item and subitem of the custom collection in one page.
I added the Custom Page Method code you posted inside site/plugins/page-method.php as is (eg without the first line being <?php), and add the navigation syntax in the template.
The output I’m having is the code in page-method.php being printed at the top of the page.
If I add <?php to the beginning of page-method.php I get this error
Argument 2 passed to Kirby::{closure}() must be an instance of Children, instance of Pages given, called in /website/kirby/vendor/getkirby/toolkit/helpers.php on line 282
The method expects an object of class Children, you are passing an object of type Pages. If you change your code so that you get a Children object, it should work:
Of course, if you omit the opening PHP tag from your code, it is rendered as plain text instead of being executed, so you can’t just leave the tag out.
Working but every link is printing only the project page url.
I think I should flatten my collection, as then I need to print out each subpage once we reach the project page.
EDIT
Oh now I see why I get always the Project page, the first and last page both direct respectively to the next and previous element. LOL, ok more green tea, it’s Sat