Skip to content

Instantly share code, notes, and snippets.

@mylesthedev
Last active March 11, 2021 13:07
Show Gist options
  • Select an option

  • Save mylesthedev/ebf1c91220f6375b7c8f to your computer and use it in GitHub Desktop.

Select an option

Save mylesthedev/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;
}
}
@Codesesh

Codesesh commented Jul 6, 2017

Copy link
Copy Markdown

Nice! Just need to remember to add .Link in the template :)

<a href="$PrevNextPage('next').Link">Next</a>

<a href="$PrevNextPage('prev').Link">Previous</a>

@pinkp

pinkp commented Mar 19, 2018

Copy link
Copy Markdown

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

nzrubin commented Aug 25, 2020

Copy link
Copy Markdown

@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