"Call to a member function hasPrevPage() on null" with template filter

Hello,

i am trying to filterby template :

  <ul>
  <?php foreach ($list = $site->children()->listed()->filterBy('template', 'product') as $item): ?>
  <li><?= $item->title() ?></li>
  <?php endforeach ?>
</ul>

<?php $pagination = $list->pagination() ?>
<nav>
  <ul>

    <?php if ($pagination->hasPrevPage()): ?>

does the pagination not work with filterby template?

Try with if condition like that:

<?php if($pagination = $list->pagination()): ?>
    // todo
<?php endif; ?>

Hi and thanks for your answer.
I don’t want to catch the error, i want to paginate thorugh my filter ;).

You have to paginate the pages before you can use the pagination object.

$list = $site->children()->listed()->filterBy('template', 'product')->paginate(3);
$pagination = $list->pagination();

An if clause will not make sense, because thepagination object will always return true.

<?php

$list = $site->children()->listed()->filterBy('template', 'product')->paginate(2);
$pagination = $list->pagination();
var_dump($list)

?>

<nav>
  <ul>

    <?php if ($pagination->hasPrevPage()): ?>
    <li>
      <a href="<?= $pagination->prevPageURL() ?>">‹prev</a>
    </li>
    <?php else: ?>
    <li>
      <span>‹</span>
    </li>
    <?php endif ?>

    <?php if ($pagination->hasNextPage()): ?>
    <li>
      <a href="<?= $pagination->nextPageURL() ?>">next›</a>
    </li>
    <?php else: ?>
    <li>
      <span>›</span>
    </li>
    <?php endif ?>

  </ul>
</nav>

no prevPageURL & hasNextPage url but the dump shows both pages.

Well, if you have two pages and you have two pages per pagination page, then there is no next page.

Maybe you want to achieve something else, but then the pagination object is not the way to go forward.

:wink: i add a third one. and yes i need both pagination and next.

works!! thanks alot!