Next,Prev Pagination through filtered Pages via Route

Hello Kirby !
Glad to be here :slight_smile:

Kirby Version: 3.3.6

I followed this guide to filter a collection of pages using a different template.

Now i need to add pagination to these pages in their single template. But now the collection isn’t filtered anymore and after using $prev and $next also other pages are shown. (obviously)…

Could you give me a hint in how to solve this ?

My route:

'pattern' => 'projekte/(:any)',
        'action'  => function ($category) {
              return Page::factory([
                  'slug' => $category,
                  'template' => 'project-category',
                  'model' => 'project-category',
                  'parent'=> page('projekte'),
                  'content' => [
                      'title' => ucfirst($category),
                  ]
              ]);              
        }

The project-category controller:

return function ($page) {

    $category = urldecode($page->slug());
    $projekte = page('projekte')->children()->filterBy('category', $category, ',')->sortBy('dateum','desc');

    return [
        'projekte' => $projekte,
        'category' => $category
    ];

};

The project controller:

return function($page) 
{
    // for sorted pagination in single project tpl
    $siblings = $page->siblings()->sortBy('datum', 'desc');
    $index  = $siblings->indexOf($page);
    
  return [
    'siblings' => $siblings,
    'index'    => $index
  ];
};

and the pagination part in the project template:

<?php if($prev = $siblings->nth($index - 1)): ?>
     <a  href="<?= $prev->url() ?>">← <?= $prev->title()->html()?></a>
<?php endif ?>        
<?php if($next = $siblings->nth($index + 1)): ?>
     <a href="<?= $next->url() ?>"><?= $next->title()->html()?> →</a>
<?php endif ?> 

Somehow i also need to apply the filter via category for the pagination in the project template …

I also tried to use these page methods (with a slightly different template for the pagination)

passing my $siblings collection with the method, but the function requires more parameters

Too few arguments to function Kirby\Cms\Page::{closure}(), 1 passed and at least 2 expected

Now iam stuck … any ideas ?
THANKS in advance …

That’s an old Kirby 2 method. There is a variant for Kirby 3, but this is not needed anymore.

The prev()/hasPrev() and next()/hasNext() methods now accept the collection as argument:

OK perfect thanks :slight_smile:

Hi,
Within a folder which contains different pages with different templates, I’m trying to use the prev/next method on a collection filtered by a specific template.

Unfortunately, it shows a last page which doesn’t belong to the collection (not in the dump).
I tried many things but without result…
Help would be much appreciated.
Thanks :pray:

  <?php
  $collection = $page->siblings()->listed()->filterBy('intendedTemplate', 'network');
  dump($collection);
  ?>
  
  <?php if($prev = $page->prev($collection)): ?>
    <a class="nav_event_prev" href="<?= $prev->url() ?>">
      <div class="round"></div>
    </a>
  <?php endif ?>

  <?php
  if($next = $page->next($collection)): ?>
    <a class="nav_event_next" href="<?= $next->url() ?>">
      <div class="round"></div>
    </a>
  <?php endif ?>

What is your Kirby version?

It’s kirby 3.
I tried to make an index but still not working
<?php $collection = $site->index()->filterBy('intendedTemplate', 'default'); ?>
I do have the right pages into the collection. Still get a page with an other template when I click on next on the last page.

There have bee quite a few Kirby 3 releases… Could you please be more specific and give use the full version number?

sorry, it’s the 3.3.1 version

Please check if the problem persists with the latest release 3.4.4, released today.

Thank you Texnixe, that solved the problem.
Sorry to bother, I should have checked this before.
Thank you for the great work on the support.

Please, how can i setup 2 paginations on a page, I have a section for “past posts” and a section for “new posts” and i want each section to have separate pagination.

It works if you use a different variable per pagination: