Jump to content

Infinite loop through sibling pages based on template


Peter Knight
 Share

Recommended Posts

Hi

I have series of staff pages and each page has a back and forward arrow to allow you to jump to the previous or next person

  • Staff overview
  • Staff 1 (template=staff-detail)
  • Staff 2 (template=staff-detail)
  • Staff 3 (template=staff-detail)
  • Staff 4 (template=staff-detail)

I had it all working as follows

<!-- START: Next and Back for staff -->
<?php
if($page->prev("template=staff-detail")->id) {
echo "<a href='{$page->prev->path}'><i class='fas fa-arrow-left'></i></a>";
} ?>
			 
<?php
if($page->next("template=staff-detail")->id) {
echo "<a href='{$page->next->path}'><i class='fas fa-arrow-right'></i></a>";
} ?>
<!-- END: Next and Back for staff -->

However, I now want to change the next and back arrows to infinitely loop through siblings.

IE

  1. if you get to Staff 4 and click next arrow, you get to Staff 1
  2. if you get to Staff 1 and click previous arrow, you get to Staff 4

I couldn't find anything in the docs which allows for some kind of infinite loop? Can anyone guide me on how to approach this?

Many thanks

P

CleanShot 2022-05-31 at 15.54.36@2x.png

Link to comment
Share on other sites

This could be a job for the Elvis operator:

//if $page->prev-path is empty, get the path of the parent's last child (sorted by sort descending), i.e. loop around to the end
echo "<a href='" . ($page->prev->path ?: $page->parent->child('sort=-sort')->path) . "'><i class='fas fa-arrow-left'></i></a>";

//if $page->next-path is empty, get the path of the parent's first child, i.e. flip over to the beginning
echo "<a href='" . ($page->next->path ?: $page->parent->child->path) . "'><i class='fas fa-arrow-right'></i></a>";

Btw in your if-condition you're checking for the next and previous sibling of a specific template, but you output the next/previous sibling without this restriction, so if you have mixed templates in this branch of your page tree, you may get unexpected results. I've left out the template conditions in my snippet above, you may want to add them in the appropriate places.

  • Like 2
Link to comment
Share on other sites

22 minutes ago, Jan Romero said:
$page->prev->path ?: $page->parent->child('sort=-sort')->path

Great solution ? Imho $page->siblings()->last() and $page->siblings()->first() would be a little bit easier to read ? 

  • Like 1
Link to comment
Share on other sites

32 minutes ago, Jan Romero said:

Ah, nice, definitely! I was hesistant to use the WireArray first() and last() functions, because I wanted to avoid loading all siblings. Not sure if that’s really a concern, though?

There will probably only be 10 siblings so OK if you're approaching from a performance point of view

Link to comment
Share on other sites

Thanks everyone.

This works great. I thought I'd have to make something much more complex.

<?php
echo "<a href='" . ($page->prev("template=staff-detail")->path ?: $page->siblings("template=staff-detail")->last->path)."'><i class='fas fa-arrow-left'></i></a>";
			
echo "<a href='" . ($page->next("template=staff-detail")->path ?: $page->siblings("template=staff-detail")->first->path) . "'><i class='fas fa-arrow-right'></i></a>";
			 ?>

The colon in the two echo statements seems to be some kind of shorthand for if/else. I hadn't seen it before so thanks for the tip.

?

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...