Hello all,
I’m currently using the following code to paginate through each of my portfolio pages:
$directionPrev = @$flip ? 'right' : 'left';
$directionNext = @$flip ? 'left' : 'right';
if($page->hasNextListed() || $page->hasPrevListed()): ?>
<nav class="pagination <?= !@$flip ?: ' flip' ?> wrap">
<?php if($page->hasPrevListed()): ?>
<a class="pagination-item <?= $directionPrev ?>" href="<?= $page->prevListed()->url() ?>" rel="prev" title="<?= $page->prevListed()->navigation()->html() ?>">
<span class="pagination-label"><?= svg("assets/images/arrow-{$directionPrev}.svg") ?><span class="pagination-label-text"><?= $page->prevListed()->navigation() ?></span></span>
</a>
<?php else: ?>
<span class="pagination-item <?= $directionPrev ?> is-inactive">
<?= svg("assets/images/arrow-{$directionPrev}.svg") ?>
</span>
<?php endif ?>
<?php if($page->hasNextListed()): ?>
<a class="pagination-item <?= $directionNext ?>" href="<?= $page->nextListed()->url() ?>" rel="next" title="<?= $page->nextListed()->navigation()->html() ?>">
<span class="pagination-label"><?= svg("assets/images/arrow-{$directionNext}.svg") ?><span class="pagination-label-text"><?= $page->nextListed()->navigation() ?></span></span>
</a>
<?php else: ?>
<span class="pagination-item <?= $directionNext ?> is-inactive">
<?= svg("assets/images/arrow-{$directionNext}.svg") ?>
</span>
<?php endif ?>
</nav>
<?php endif ?>
While this works well, navigation only works in a single direction (forwards or backwards). When the first or last item is reached it’s only possible to navigate in the opposite direction. What I would like is an infinite loop - when the last item is reached, navigation loops back to the first item (and vice versa when the first item is reached).
I found the following post Previous / Next Navigation Loop and code…
<?php if ($prev = $page->prevListed() ?? $page->siblings()->listed()->last()): ?>
<a href="<?= $prev->url() ?>">previous page</a>
<?php endif ?>
<?php if ($next = $page->nextListed() ?? $page->siblings()->listed()->first()): ?>
<a href="<?= $next->url() ?>">next page</a>
<?php endif ?>
…which I found helpful. However, while I think those else statements need to be removed, I can’t wrap my head around the syntax to make this work with my current code.
Could someone point me in the right direction please?
Thank you.