Latest

Thursday, July 6, 2017

Get next and previous subpage link in pages october cms plugin

Asked by: adam


I've a structure like this in the backend of october cms (static pages plugin)

Page
-subpage 1
-subpage 2
-subpage 3

I want to be able to link between the child pages to go to the next and previous one (if such exists).

Could not find anything about this.

OK. This is what I've got - not the most elegant solution but it works In the code part of the subpage! (the're sould be a check i the page has a parent but in my situation I only use links to subpages)

function onStart(){
  // current page url
  $parent = $this->page['apiBag']['staticPage']->getParent();
  $url = $this->page['apiBag']['staticPage']['viewBag']['url'];

  $currentPage = null;
  $children = $parent->getChildren();

  foreach( $children as $key => $page){
    if($page['viewBag']['url'] == $url) $currentPage = $key;
  }

  // previous page
  if ( array_key_exists($currentPage - 1, $children) ) {
    $this['prev_url'] = $children[$currentPage - 1]['viewBag']['url'];
    $this['prev_title'] = $children[$currentPage -1 ]['viewBag']['title'];
  }

  if ( array_key_exists($currentPage + 1, $children) ) {
    $this['next_url'] = $children[$currentPage + 1]['viewBag']['url'];
    $this['next_title'] = $children[$currentPage + 1]['viewBag']['title'];
  }

}


Source

No comments:

Post a Comment

Adbox