hasPrev/NextListed() in collection

Hi there,

I am using the hasPrevListed/hasNextListed function to checks if there’s a previous listed page in the siblings collection:


This works fine until I get into the last or first project of a collection. Then he/she (the website) moves on to another collection/category.
Does anyone know how to prevent this?

And in addition to this question. A project can has multiple categories. I only want to show the next project in the category (collection) in which the visitor has entered the project. Is this even possible?

My code:

    <ul class="pagination">

        <?php $collection = $page->siblings()->listed(); if ($page->hasPrevListed($collection)): ?>
        <li class="prev">
            <a href="<?= $page->prevListed($collection)->url() ?>"><span class="text">Previous project</span> </a>
        </li>
        <?php endif ?>

        <?php $collection = $page->siblings()->listed(); if ($page->hasNextListed($collection)): ?>
        <li class="next">
            <a href="<?= $page->nextListed($collection)->url() ?>"><span class="text">Next project</span> </a>
        </li>
        <?php endif ?>

    </ul>

Thanks!

There’s an error in your code, you define your collection twice. Should be like this:

<?php $collection = $page->siblings()->listed(); ?>

<ul class="pagination">

  <?php if ($page->hasPrevListed($collection)) : ?>
    <li class="prev">
      <a href="<?= $page->prevListed($collection)->url() ?>"><span class="text">Previous project</span> </a>
    </li>
  <?php endif ?>

  <?php if ($page->hasNextListed($collection)) : ?>
    <li class="next">
      <a href="<?= $page->nextListed($collection)->url() ?>"><span class="text">Next project</span> </a>
    </li>
  <?php endif ?>

</ul>

If a URL parameter is set, you can filter by that when defining your collection:

$collection = $page->siblings()->listed()->filterBy('category', param('category'));

Ideally, you would do this conditionally in your controller.

1 Like

Thank you for you help!
I did not set a url parameter. So I’m afraid it’s a bit more complicated.

Yep, the category parameter was nonsense. You need to pass the category via the link that is clicked when the user opens the project page. How do you filter on the parent page then? Via a route?

Yes. So I need to make a kind of ‘custom’ linke with a parameter. I filter my page with a route indeed.