Paginate by months

Not sure if this is possible which is why I’m asking: is there a way to paginate a collection by month rather than by a fixed number of pages?

So page:1 is all posts from April, page:2 all posts from March and so on.

I would say it’s no longer pagination then. It’s more filtered sets, which should be possible: https://getkirby.com/docs/cookbook/content/filtering

If you combine the filtering with links to the previous/next month, you should be able to achieve something similar to standard pagination.

@bvdputte well it still divides the content into pages so it’s still technically a pagination even though it’s not a regular one.

The standard pagination only works with a given limit

I managed to improve the code using the Pagination class:

<?php

return function ($kirby, $page) {

    $groups = $page->children()->sortBy('date')->group(function($item) {
        return $item->date()->toDate('m-Y');
    });
   
    $pagination = Pagination::For($groups, ['limit' => 1]);    
    
    $groups = $groups->slice($pagination->offset(), $pagination->limit());
  
   return compact( 'groups', 'pagination');
};
<ul class="albums">
    <?php foreach ($groups as $key => $group): ?>
    <?php foreach($group as $album): ?>
    <li>
      <a href="<?= $album->url() ?>">
        <figure>
          <?php if ($cover = $album->cover()): ?>
          <?= $cover->crop(800, 1000) ?>
          <?php endif ?>
          <figcaption><?= $album->title() ?></figcaption>
        </figure>
      </a>
    </li>
    <?php endforeach ?>
    <?php endforeach ?>
  </ul>
  <?php if($pagination->hasPages()): ?>
  <nav>
    <a href="<?= $pagination->nextPageUrl() ?>">Next</a>
    <a href="<?= $pagination->prevPageUrl() ?>">Prev</a>
  </nav>
  <?php endif ?>
1 Like

Definitely going to give this a try. I’ll let you know if it works on my site.
And thank you :heart: