Jump to content

If page is last, show link to first setup


a-ok
 Share

Recommended Posts

Hi folks,

I'm set up a simple next/prev between pages that are loaded in via pjax with the following:

<?php if ($page->next->id) : ?>
    <div class="next-solution-container">
        <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a>
    </div>
<?php endif; ?>

This worked great, but I want it now to loop... so if it's the last item, it gets the first item as the next link. I thought the following below would work, but as I am using it on the page template, and thus using $page, I don't think first/last are in use?

<?php if ($page == $page->last) : ?>
    <div class="next-solution-container">
        <a data-pjax class="next-solution" href="<?php echo $page->first->url; ?>"><span><?php echo $page->first->title; ?></span></a>
    </div>
<?php else : ?>
    <div class="next-solution-container">
        <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a>
    </div>
<?php endif; ?>

Any thoughts?

Link to comment
Share on other sites

Try $page == $page->parent->children->last().

EDIT: Gah, there's a way to do it with siblings() where it includes the current page in the siblings() PageArray but the correct code eludes me and I suspect diogo will beat me to it :)


Oooh, you could do

if ($page->next) {
  // show the next link - $page->next;
} else {
  // show the link to the first again - $page->siblings->first();
}

I think.

  • Like 3
Link to comment
Share on other sites

You might also, depending on your scenario, want to change the "else" in my scenario to check for other pages at the same level as this page and not show any links if there are no other pages, so I think this would work for that scenario:

if ($page->next) {
    // show the next link - $page->next;
} elseif ($page->siblings) {
    // show the link to the first again - $page->siblings->first();
}

Or for something that reads a little more clearly but does the same:

if ($page->siblings) { // Only bother with next/back-to-first links if this page isn't all on its own
    if ($page->next) {
        // show the next link - $page->next;
    } else {
        // show the link to the first again - $page->siblings->first();
    }
}
  • Like 1
Link to comment
Share on other sites

This worked for me in my scenario, but thanks for the others too. Really appreciate the help.

Try $page == $page->parent->children->last().

EDIT: Gah, there's a way to do it with siblings() where it includes the current page in the siblings() PageArray but the correct code eludes me and I suspect diogo will beat me to it :)


Oooh, you could do

if ($page->next) {
  // show the next link - $page->next;
} else {
  // show the link to the first again - $page->siblings->first();
}

I think.

Link to comment
Share on other sites

@mr-fan - Please feel free to add it there as I need to sleep now, mega-busy tomorrow and will be away for a week from Saturday - glad I could help :)

@tpr - if you're viewing the first item, the link for Next will show to take you to the second item. I think maybe what you mean though is when viewing the first item should there be a "previous" link to the last item for consistency? I may be completely misunderstanding your question of course.

Link to comment
Share on other sites

@Pete

If you reach the final page, then you use $page->siblings->first, that's fine.

My concern is that does the "iterator" reset too? So after calling first(), will the current page be the first page, and calling next afterwards would be the second one?

The OP wrote he is calling these pages via ajax, so the current $page imo wouldn't be the first one.

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

×
×
  • Create New...