Pagination within a Merged Collection

I used the conversation here to help structure my website’s homepage (displaying all subpages in Blog and Projects, sorted by date), but I am unable to figure out how pagination would work using the custom collection. Any help here would be greatly appreciated!

<?php
    $posts_a = $site->find('blog')->children();
    $posts_b = $site->find('projects')->children();
    $posts = new Pages(array($posts_a, $posts_b));
?>
		
<?php foreach($posts->sortBy('date', 'desc', 'time', 'desc')->paginate(10)->visible() as $post): ?>
		
    <?php if($post->template() == 'article.text'): ?>

    <?php elseif($post->template() == 'article.quote'): ?>

    <?php elseif($post->template() == 'project'): ?>

    <?php endif ?>
				
<?php endforeach ?>

I used the Pagination Post in the Kirby Docs, and altered the foreach statement as instructed, but it doesn’t seem to work. I’m sure there is something simple I’m missing, but I can’t seem to figure out what I need to change.


Update: Using the following, I am able to get the Next and Previous buttons to show, but the URLs do not point anywhere.

<?php if($post->pagination()->hasPages()): ?>
<nav class="pagination">

<?php if($post->pagination()->hasNextPage()): ?>
<a class="next" href="<?php echo $post->pagination()->nextPageURL() ?>">&lsaquo; older posts</a>
<?php endif ?>

<?php if($post->pagination()->hasPrevPage()): ?>
<a class="prev" href="<?php echo $post->pagination()->prevPageURL() ?>">newer posts &rsaquo;</a>
<?php endif ?>

</nav>
<?php endif ?>

Your variable in the pagination nav seems to be wrong, it should be $posts instead of $post everywhere.

Also, in this line

foreach($posts->sortBy(‘date’, ‘desc’, ‘time’, ‘desc’)->paginate(10)->visible() as $post): ?>

filter by visible() before you sort and paginate:

foreach($posts->visible()->sortBy('date', 'desc', 'time', 'desc')->paginate(10) as $post): ?>

Changing $post to $posts makes the Next and Previous links disappear (even though they don’t point anywhere). Navigating to /page:2 shows the posts in the right order, but I am unable to link to it automatically.

I also just noticed that the Pagination code causes the Footer not to display. I’m not sure if this is a symptom of something else going wrong.

Yeah, I overlooked something, the new $posts collection needs to be paginated:

<?php
    $posts_a = $site->find('blog')->children()->visible();
    $posts_b = $site->find('projects')->children()->visible();
    $posts = new Pages(array($posts_a, $posts_b));
    $posts = $posts->paginate(10);
    $pagination = $posts->pagination();
?>

<?php foreach($posts as $post): ?>
		
    <?php if($post->template() == 'article.text'): ?>

    <?php elseif($post->template() == 'article.quote'): ?>

    <?php elseif($post->template() == 'project'): ?>

    <?php endif ?>
				
<?php endforeach ?>

<?php if($pagination->hasPages()): ?>
<nav class="pagination">

  <?php if($pagination->hasNextPage()): ?>
  <a class="next" href="<?php echo $pagination->nextPageURL() ?>">&lsaquo; older posts</a>
  <?php endif ?>

  <?php if($pagination->hasPrevPage()): ?>
  <a class="prev" href="<?php echo $pagination->prevPageURL() ?>">newer posts &rsaquo;</a>
  <?php endif ?>

</nav>
<?php endif ?>
1 Like

I would have never figured that out! Thank you so much – works perfectly!! :slight_smile: