Pagination link throws error 404

Hi there.

My problem:
I have a blog.php template (and a blog.php controller) who displays my blogposts or sort them per category.

When i reach my pagination limit i can see my pagination nav but get a 404 error when i click on older posts.
My category list then shows the pagination too, even if the pagination limit is not reached for the displayed articles per category.

There is no error in debug mode.

My code:
I have the following code in my template blog.php:

<?php if($pagination->hasPages()): ?>
  <nav class="pagination">
    <?php if($pagination->hasNextPage()): ?>
    <a href="<?= $pagination->nextPageURL() ?>" class="pagination__older">Ältere Einträge</a>
    <?php endif ?>
    <?php if($pagination->hasPrevPage()): ?>
    <a href="<?= $pagination->prevPageURL() ?>" class="pagination__newer">Neuere Einträge</a>
    <?php endif ?>
  </nav>
<?php endif ?>

Then i have the controller blog.php:

<?php

return function($site, $pages, $page) {

  // Kategorien
  $category = urldecode(param('category'));
  $articlesByCategory = $pages->index()
                              ->visible()
                              ->filterBy('category', $category)
                              ->flip()
                              ->paginate(10);

  // Artikel
  $articles = $pages->index()
                    ->visible()
                    ->filterBy('template', 'post.article' && 'post.link' && 'post.image' && 'post.status')
                    ->flip()
                    ->paginate(10);

  return array(
    'category' => $category,
    'articlesByCategory' => $articlesByCategory,
    'articles' => $articles,
    'pagination' => $articles->pagination()
  );
};

What is in your blog.php template? Somehow it doesn’t make sense to me that you have two separate collections, $articles (with a pagination object) and $articlesByCategory (without a pagination object).

In my blog.php template i have:

if (param('category')):  // show articles by category ?>
<?php foreach($articlesByCategory as $article): ?>
...
<?php endforeach  ?>

<?php else: // show articles ?>

<?php foreach($articles as $article): ?>
...
<?php endforeach ?>

<?php endif ?>

And then the pagination as seen above.

Well, yes, but your pagination object only refers to the $articles collection, I recommend you do it as explained in the docs: https://getkirby.com/docs/cookbook/tags/#blog-controller

I’ve got it. Thank you.