【问题标题】:Get next and previous subpage link in pages october cms plugin获取页面 october cms 插件中的下一个和上一个子页面链接
【发布时间】:2017-07-06 10:32:32
【问题描述】:

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

}

【问题讨论】:

  • 好吧 .. 那你为什么不创建自己的博客类型的插件 .. 在其中添加记录并通过查询下一个和上一个链接来检查 ..

标签: php laravel octobercms octobercms-plugins


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2010-11-18
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多