2011-08-13 126 views
0

如何顯示節點模板中的父菜單項?如何在節點模板中顯示父菜單項? Drupal 7

我想顯示父菜單項目與當前頁面;但我不需要別人。

編輯:我啓用菜單面包屑模塊​​,並添加以下代碼:

<?php    
       $menuParent = menu_get_active_trail(); 
       if (sizeof ($menuParent) >= 2 && $menuParent[2]) { 
        $menuParent = $menuParent[1]['link_title']; 
        print $menuParent; 
       } 
      ?> 

它工作正常,但我得到一個錯誤的不具有二級導航的頁面: 錯誤:注意:未定義偏移量:2包含()

我認爲我的條件sizeof將照顧問題,但不工作。

回答

0

您正在檢查$menuParent[2],但後來使用$menuParent[1]。也許檢查$menuParent[1]

if (sizeof ($menuParent) >= 2 && $menuParent[1]) { 

PHP的數組是零索引,所以槽1是第二個插槽。

8

使用PHP陣列的工具,讓你正確的項目在數組中:

<?php 
    $menuParent = menu_get_active_trail(); 
    //get rid of the last item in the array as it is the current page 
    $menuParentPop = array_pop($menuParent); 
    //Just grab the last item in the array now 
    $menuParent = end($menuParent); 
    //if it is not the home page and it is not an empty array 
    if(!empty($menuParent) && $menuParent['link_path'] != ''){ 
    print $menuParent['title']; 
    } else{ 
    print $title; 
    } 
?> 
+0

如果你想獲得菜單的根路徑,應該在'$ menuParent [1]'。 $ menuParent [0]將始終返回首頁,因此如果在頁面上嘗試訪問它,請確保檢查$ menuParent [1]是否已設置 –