Skip to content

Instantly share code, notes, and snippets.

@mediabeastnz
Last active March 11, 2021 13:07
Show Gist options
  • Save mediabeastnz/ebf1c91220f6375b7c8f to your computer and use it in GitHub Desktop.
Save mediabeastnz/ebf1c91220f6375b7c8f to your computer and use it in GitHub Desktop.
SilverStripe Prev / Next Controls
public function PrevNextPage($Mode = 'next') {
if($Mode == 'next'){
return SiteTree::get()->filter(array("ParentID" => $this->ParentID, "Sort:GreaterThan" => $this->Sort))->sort("Sort ASC")->limit(1)->first();
}
elseif($Mode == 'prev'){
return SiteTree::get()->filter(array("ParentID" => $this->ParentID, "Sort:LessThan" => $this->Sort))->sort("Sort DESC")->limit(1)->first();
}
else{
return false;
}
}
@pinkp
Copy link

pinkp commented Mar 19, 2018

Is it possible to amend this so that IF you are on the last page that instead of returning false and therefor no link it takes you back to the first page (looping essentially)?

@nzrubin
Copy link

nzrubin commented Aug 25, 2020

@pinkp
To avoid the "Previous" button on the first page a and "Next" at the last:

<% if Parent && $ChildrenOf($Parent.URLSegment).count > 1 %> <div> <% if $Sort > 1 %> <% loop PrevNextPage(prev) %> <a href="$Link">&lt; Previouse</a> <% end_loop %> <% end_if %> <% if $Sort < $ChildrenOf($Parent.URLSegment).count %> <% loop PrevNextPage(next) %> <a href="$Link">Next &gt; </a> <% end_loop %> <% end_if %> </div> <% end_if %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment