Links to sibling page in collection – closing the loop

Hi everyone!

A pretty simple question.

I have a collection page (say for ‘animals’), and its children pages (‘pelican’, ‘lion’, etc).

I’m trying to add next/page links to sibling pages in a same collection. I put together this, which seems to work fine:

{% if page.isLast(page.path) == false %}
  <a href="{{page.prevSibling(page.path).url }}">next</a>
{% endif %}
{% if page.isFirst(page.path) == false %}
  <a href="{{page.nextSibling(page.path).url }}">prev</a>
{% endif %}

My question is: if the current page is, say, the first in the collection (‘Antelope’), how can I add a rule so that prev links to the last page in the collection (say, ‘West Highland Terrier’)? And vice versa.

Cheers,

@PurpleRoom,

which seems to work fine

Yes, that seems to work fine, but only because PHP is so forgiving and doesn’t throw errors on incorrect code…

Class Page (see docs) does not have a function Page::isLast(string $path), only Page::isLast(). PHP will happily ignore the parameter.

Back to the question…

Looking at the implementation of Page::isFirst(), it will do the following:

public function isFirst()
{
   $parent = $this->parent();
   $collection = $parent ? $parent->collection('content', false) : null;
   if ($collection instanceof Collection) {
        return $collection->isFirst($this->path());
   }

   return true;
}

Look at the docs about Collections you will find the following:

|Collection::first()|Get the first item in the collection|
|Collection::last()|Get the last item in the collection|

When combining the two, when getting the collection of the parent, you can also get the first and last element in the collection.

{{ page.parent.collection.first.url }}
{{ page.parent.collection.last.url }}

Note:

  • I think you want to create some circular looping through siblings. I don’t know your use-case, but I’m afraid this is confusing to the end-user. When will the end-user come to the conclusion that he/she is looping?
  • It might be that pagination might throw of the first and last item in the collection. Just try and see what happens.
1 Like

Yes, that seems to work fine, but only because PHP is so forgiving and doesn’t throw errors on incorrect code…

:sweat_smile:

Dear @pamtbaau, you are the forgiving one!

In all seriousness, many, many thanks for taking the time for this excellent explanation, and pointing me to the correct solution.

A great day to you!

Oh and just to answer on the side notes:

I think you want to create some circular looping through siblings. I don’t know your use-case, but I’m afraid this is confusing to the end-user. When will the end-user come to the conclusion that he/she is looping?

You are right to raise the issue. In this case the correct succession of pages is not meaningful or important, so it’s more about providing a convenient navigation.

It might be that pagination might throw of the first and last item in the collection. Just try and see what happens.

Looks perfect!