2017-07-06 71 views
1

我在十月CMS的後端像這樣的結構(靜態網頁插件)獲取下一個和頁面十月CMS以前的子頁面鏈接插件

Page 
-subpage 1 
-subpage 2 
-subpage 3 

我希望能夠在子頁面之間的鏈接去下一個和上一個(如果存在)。

找不到任何關於此的內容。

好的。這是我得到的 - 不是最優雅的解決方案,但它的工作原理 在子頁面的代碼部分! (the're應該是一個支票,我的網頁有一個父母,但在我的情況下,我只使用鏈接到子頁面)

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

} 
+0

嗯..然後你爲什麼不建立自己的博客類型的插件..在其中您可以添加記錄,並通過使查詢一個和前一個鏈接檢查.. –

回答

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