Peter Knight Posted May 31, 2022 Share Posted May 31, 2022 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 if you get to Staff 4 and click next arrow, you get to Staff 1 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 Link to comment Share on other sites More sharing options...
Jan Romero Posted May 31, 2022 Share Posted May 31, 2022 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. 2 Link to comment Share on other sites More sharing options...
bernhard Posted May 31, 2022 Share Posted May 31, 2022 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 ? 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted May 31, 2022 Share Posted May 31, 2022 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? 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted May 31, 2022 Author Share Posted May 31, 2022 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 More sharing options...
Peter Knight Posted May 31, 2022 Author Share Posted May 31, 2022 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. ? 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now