Pagination not working 100%

Hi all,

I’m having trouble with setting up the pagination on my blog janwillem.net.

The pagination on the blog (homepage) works, but only if I manually type in the address of the website and then add /page:2
When you look at the navigation links at the bottom of the blog, the address is just the homepage of the blog and not /page:2

Here is my home.php code:

<?php snippet('header') ?>

<?php foreach (page('notes')->grandChildren()->listed()->sortBy('date', 'desc')->paginate(2) as $note): ?>

<article>

<?php if($note->toggle() == "true"): ?>
	<titel><?= $note->title()->html() ?></titel>
<?php endif ?>

<?= $note->text()->kt() ?> 
<date>
	<a href="<?= $note->url() ?>">
		<?= $note->date()->toDate('Y-m-d') ?>
	</a>
</date>

</article>

<?php endforeach ?>

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

  <?php if ($note->pagination()->hasNextPage()): ?>
  <a class="next" href="<?= $note->pagination()->nextPageURL() ?>">
    ‹ older posts
  </a>
  <?php endif ?>

  <?php if ($note->pagination()->hasPrevPage()): ?>
  <a class="prev" href="<?= $note->pagination()->prevPageURL() ?>">
    newer posts ›
  </a>
  <?php endif ?>

</nav>
<?php endif ?>
  
<?php snippet('footer') ?>

You are trying to paginate a single note, which doesn’t work. You have to call the pagination() method on the collection:

<?php
$notes = page('notes')->grandChildren()->listed()->sortBy('date', 'desc')->paginate(2);
$pagination = $notes->pagination();
foreach ($notes as $note): ?>
<!-- rest of code -->
<?php endforeach ?>

And then

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

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

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

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

pfffff… Thank you so much! :muscle:t2: :muscle:t2:

And extra thanks for the quick reply.