获取页面十月CMS插件中的下一个和上一个子页面链接



我在十月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'];
}
}

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

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%}

最新更新