1

我在 october cms 的后端有这样的结构(静态页面插件)

Page
-subpage 1
-subpage 2
-subpage 3

我希望能够在子页面之间链接以转到下一个和上一个(如果存在)。

找不到任何关于此的内容。

好的。这就是我所拥有的——不是最优雅的解决方案,但它可以在子页面的代码部分中工作!(应该检查一下我的页面是否有父页面,但在我的情况下,我只使用指向子页面的链接)

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'];
  }

}
4

1 回答 1

0

添加了父页面。现在它也适用于二级页面。

function onStart()
{
    $this['search_query'] = get('q', $default = null);
    // current page url
    $parent = $this->page['apiBag']['staticPage']->getParent();
    $url = $this->page['apiBag']['staticPage']['viewBag']['url'];

    $currentPage = null;
    if($parent) {
        $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'];
        }
    // parent page
    $this['parent_title'] = $parent['viewBag']['title'];
    $this['parent_url'] = $parent['viewBag']['url'];
    }
}

枝条:

{% if prev_title|length > 0 %}
<a href="{{ prev_url }}" class="previous">{{ prev_title }}</a>
{% endif%}
{% if parent_title|length > 0 %}
<a href="{{ parent_url }}" class="up">{{ parent_title }}</a>
{% endif%}
{% if next_title|length > 0 %}
<a href="{{ next_url }}" class="next">{{ next_title }}</a>
{% endif%}
于 2017-10-05T13:43:37.920 回答