# How to make Prevnext loop?

**URL:** <https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 2, 2016, 12:03pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928 "2016-12-02T12:03:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toboggan](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@Toboggan](https://forum.getkirby.com/u/Toboggan)\
**Post date:** [December 2, 2016, 12:03pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/1 "2016-12-02T12:03:00Z")

</div>

Hi,

I’m slowly getting to know Kirby (and PHP) and have a question about a snippet included in the Starter Kit:

How to make the prevnext-snippet loop from last project back to first (and vice versa)?

I tried searching similar topics, but didn’t find anything that would apply directly to this question.

Thank you for any advice already in advance!

---

<div class="post-metadata">

**Author:** ![seb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/seb/32/3296_2.png) [@seb](https://forum.getkirby.com/u/seb)\
**Post date:** [December 2, 2016, 12:05pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/2 "2016-12-02T12:05:41Z")

</div>

Try `<?php foreach ($page->siblings() as $project): ?>`🙂

That grabs all the [siblings](https://getkirby.com/docs/cheatsheet/page/siblings).

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [December 2, 2016, 12:13pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/3 "2016-12-02T12:13:36Z")

</div>

I wrote a plugin for it a while ago. I hope it helps: [https://github.com/jenstornell/kirby-pagejump](https://github.com/jenstornell/kirby-pagejump)

---

<div class="post-metadata">

**Author:** ![Toboggan](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@Toboggan](https://forum.getkirby.com/u/Toboggan)\
**Post date:** [December 2, 2016, 12:25pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/4 "2016-12-02T12:25:03Z")

</div>

Thanks Seb and jenstornell for your quick replies! 😌

Since I’m still taking baby steps in both Kirby and PHP here are some follow-up questions:

jenstornell: Your plugin looks like what I’m searching for, but is there a simple way to apply it to the Starter Kit Project-template?

Seb: Where would you place the code in the original snippet?

```php
$directionPrev = @$flip ? 'right' : 'left';
$directionNext = @$flip ? 'left' : 'right';

if($page->hasNextVisible() || $page->hasPrevVisible()): ?>
  <nav class="pagination <?= !@$flip ?: ' flip' ?> wrap cf">

    <?php if($page->hasPrevVisible()): ?>
      <a class="pagination-item <?= $directionPrev ?>" href="<?= $page->prevVisible()->url() ?>" rel="prev" title="<?= $page->prevVisible()->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionPrev}.svg"))->content() ?>
      </a>
    <?php else: ?>
      <span class="pagination-item <?= $directionPrev ?> is-inactive">
        <?= (new Asset("assets/images/arrow-{$directionPrev}.svg"))->content() ?>
      </span>
    <?php endif ?>

    <?php if($page->hasNextVisible()): ?>
      <a class="pagination-item <?= $directionNext ?>" href="<?= $page->nextVisible()->url() ?>" rel="next" title="<?= $page->nextVisible()->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionNext}.svg"))->content() ?>
      </a>
    <?php else: ?>
      <span class="pagination-item <?= $directionNext ?> is-inactive">
        <?= (new Asset("assets/images/arrow-{$directionNext}.svg"))->content() ?>
      </span>
    <?php endif ?>

  </nav>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![seb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/seb/32/3296_2.png) [@seb](https://forum.getkirby.com/u/seb)\
**Post date:** [December 2, 2016, 12:44pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/5 "2016-12-02T12:44:39Z")

</div>

Well, it depends on what you want and where you want to add it.

This:

```
<ul>
<?php foreach ($page->siblings() as $project): ?>
  <li><?= $project->name(); ?></li>
<?php endforeach; ?>
</ul>

```

… just gives a list of all the names of all the siblings. You can add `<a href="">` and stuff yourself. Use `<?= $project->url() ?>` to get it’s url. Just try yourself and learn ^^

---

<div class="post-metadata">

**Author:** ![Toboggan](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@Toboggan](https://forum.getkirby.com/u/Toboggan)\
**Post date:** [December 2, 2016, 1:30pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/6 "2016-12-02T13:30:33Z")

</div>

Thank you for the pointer, seb!

I’ll look into this and see if I can make it work! 🙏

---

<div class="post-metadata">

**Author:** ![lukaskleinschmidt](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukaskleinschmidt/32/13388_2.png) [@lukaskleinschmidt](https://forum.getkirby.com/u/lukaskleinschmidt)\
**Post date:** [December 2, 2016, 6:48pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/7 "2016-12-02T18:48:36Z")

</div>

Don’t know if i got your question right but you can simply do something like this.

```auto
<?php
$directionPrev = @$flip ? 'right' : 'left';
$directionNext = @$flip ? 'left' : 'right';

if($page->hasNextVisible() || $page->hasPrevVisible()): ?>
  <nav class="pagination <?= !@$flip ?: ' flip' ?> wrap cf">

    <?php if($prev = $page->prevVisible()): ?>
      <a class="pagination-item <?= $directionPrev ?>" href="<?= $prev->url() ?>" rel="prev" title="<?= $prev->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionPrev}.svg"))->content() ?>
      </a>
    <?php else: ?>
      <a class="pagination-item <?= $directionPrev ?>" href="<?= $page->siblings()->last()->url() ?>" rel="prev" title="<?= $page->siblings()->last()->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionPrev}.svg"))->content() ?>
      </a>
    <?php 
    <?php endif ?>

    <?php if($next = $page->nextVisible()): ?>
      <a class="pagination-item <?= $directionNext ?>" href="<?= $next->url() ?>" rel="next" title="<?= $next->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionNext}.svg"))->content() ?>
      </a>
    <?php else: ?>
      <a class="pagination-item <?= $directionNext ?>" href="<?= $page->siblings()->first()->url() ?>" rel="next" title="<?= $page->siblings()->first()->title()->html() ?>">
        <?= (new Asset("assets/images/arrow-{$directionNext}.svg"))->content() ?>
      </a>
    <?php endif ?>

  </nav>
<?php endif ?>

```

Edit added it to you code but untested.

---

<div class="post-metadata">

**Author:** ![seb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/seb/32/3296_2.png) [@seb](https://forum.getkirby.com/u/seb)\
**Post date:** [December 2, 2016, 11:52pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/8 "2016-12-02T23:52:43Z")

</div>

Ah THAT was the question. Pardon me. I should read before I try to help.

---

<div class="post-metadata">

**Author:** ![Toboggan](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@Toboggan](https://forum.getkirby.com/u/Toboggan)\
**Post date:** [December 3, 2016, 1:37pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/9 "2016-12-03T13:37:06Z")

</div>

Thank you lukaskleinschmidt,

This was exactly what I tried to accomplish! It’s also a very useful example for understanding any similar adjustments in the future. There was an extra “\<?php” in the middle of the code but that was thankfully easy spot 🕵️‍♂️

Question solved 😌

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:54pm UTC](https://forum.getkirby.com/t/how-to-make-prevnext-loop/5928/10 "2025-02-06T16:54:41Z")

</div>


