# Prev() and next() with custom collection

**URL:** <https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 14, 2015, 5:03pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823 "2015-12-14T17:03:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakobploens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jakobploens/32/1315_2.png) [@jakobploens](https://forum.getkirby.com/u/jakobploens)\
**Post date:** [December 14, 2015, 5:03pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/1 "2015-12-14T17:03:36Z")

</div>

Hey guys,

as it is written in [this post](https://forum.getkirby.com/t/replacement-for--next-and--prev/2176), the functions `_next()` and `_prev()` used to do what I am trying to accomplish 😃 Also it seems I am not the only one – [Previous/next article within specific tags?](https://forum.getkirby.com/t/previous-next-article-within-specific-tags/747).

So here’s the thing: I want to use `next()` and `prev()` functions, together with `hasNext()`/`hasPrev()` and so on – but in a custom collection, for example just the images of my folder. But with next() and prev() I always get all files/siblings from the parent collection, without filtering something out. For the moment I hack the code to make those functions not protected anymore, but of course it would be great if there would be a solution directly in the core, without modifying it…

If there’s not an existing feature I can use in this case, I’ll post this as an suggestion, but I wanted to make sure I haven’t failed to see something obvious 😉 Or has anybody another idea how to accomplish this in an easy way?

Thank you!

**Edit:**  
Ha, downside! Those functions are only made for pages, not for files… So more than ever I’m searching for a solution without modifying the core to much 😄

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 14, 2015, 11:06pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/2 "2015-12-14T23:06:54Z")

</div>

Don’t know if I get you right, but you can do this to show only one (or more) images per page (you can filter images as well in this loop, if you want):

```php
<?php $images = $page->images->paginate(1);
  $pagination = $images->pagination();
  foreach($images as $image) :?>
    <img src="<?= $image->url() ?>">
  <?php endforeach ?>

<?php if($pagination->hasPrevPage()): ?>
<a href="<?= $pagination->prevPageUrl() ?>">
  prev (<?= $pagination->prevPage() ?>)
</a>
<?php endif ?>

<?php if($pagination->hasNextPage()): ?>
<a href="<?= $pagination->nextPageUrl() ?>">
  next (<?= $pagination->nextPage() ?>)
</a>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![jakobploens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jakobploens/32/1315_2.png) [@jakobploens](https://forum.getkirby.com/u/jakobploens)\
**Post date:** [December 14, 2015, 11:32pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/3 "2015-12-14T23:32:58Z")

</div>

Hm, no that’s not quite my use case. I have different pages (projects), each with multiple files insight. Sometimes only images, sometimes only videos, sometimes both and documents, … . I open the “file view” by appending a parameter to the page url (file), so if `param('file')` returns a value (and a file is found), the file view is shown. There I have a simple arrow-navigation for prev/next file – but those should only navigate within the same type of file, so if an image is open, just navigate between the project’s images, and ignore videos and documents. So I need some kind of backwards mechanism – select all files with the same file type like the open one.

My solution for the moment was indeed just copy-pasting the `_next`/`_prev`-functions to _kirby/core/file.php_, and adjusting the first parameter to take only `Files` instead of `Children`. It works like a charm now, but unfortunately it’s only possible with modifying the core… is there an obvious reason I’m missing why the `_next`/`_prev`-functions are a) only available for a page and not for files, and why they are b) protected and not “officially” supported? I mean, even the usual `next` and `prev` function of a page just point to those functions… 😉 Wouldn’t it be the best if they would live inside an `Obj`?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 18, 2016, 1:58pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/4 "2016-05-18T13:58:55Z")

</div>

With Kirby 2.3 you can now achieve the same with custom page, file etc. methods, just tested this for a project and it works well.

I have no idea why those methods are protected, maybe because they are only useful if you have a method to keep your original collection by either using an URL parameter, or storing those values in the session, or using cookies …

---

<div class="post-metadata">

**Author:** ![jakobploens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jakobploens/32/1315_2.png) [@jakobploens](https://forum.getkirby.com/u/jakobploens)\
**Post date:** [May 18, 2016, 2:02pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/5 "2016-05-18T14:02:15Z")

</div>

Thanks for keeping this up to date! Awesome service, as always 😉

---

<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:55pm UTC](https://forum.getkirby.com/t/prev-and-next-with-custom-collection/2823/6 "2025-02-06T16:55:27Z")

</div>


