Jump to:

5102 Posts in 1520 Topics by 1116 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Page Controls: Next/Previous Page Link?

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4488 Views
  • fabilo
    Avatar
    Community Member
    10 Posts

    Re: Page Controls: Next/Previous Page Link? Link to this post

    cbolt's solution worked well for me. I made two minor edits...

    PreviousPage function would fail if Children return a dataobjectset with no children. So updated the function to use Count():

    function PreviousPage() {
    $where = "ParentID = {$this->ParentID} AND Sort < {$this->Sort}";
    $pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
    if($pages) {
    foreach($pages as $page) {
    // if page has a child go to the last child page
    $children = $page->AllChildren();
    if ($children->Count()) {
    foreach ($children as $child) {
    continue;
    }
    return $child;
    }
    return $page;
    }
    }

    I also updated both functions Where clauses to only return pages shown in the menu. ie:

    $where = "ParentID = $parent AND `SiteTree`.ID != 4 AND Sort > $sort AND ShowInMenus = 1";

    4488 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.