Drupal 7 如何在节点模板中显示父菜单项?德鲁帕尔7
如何在节点模板中显示父菜单项 我想在当前页面中显示父菜单项;但是我不需要别人 编辑:我启用了菜单面包屑模块,并添加了以下代码:Drupal 7 如何在节点模板中显示父菜单项?德鲁帕尔7,drupal-7,Drupal 7,如何在节点模板中显示父菜单项 我想在当前页面中显示父菜单项;但是我不需要别人 编辑:我启用了菜单面包屑模块,并添加了以下代码: <?php $menuParent = menu_get_active_trail(); if (sizeof ($menuParent) >= 2 && $menuParent[2]) { $menuPare
<?php
$menuParent = menu_get_active_trail();
if (sizeof ($menuParent) >= 2 && $menuParent[2]) {
$menuParent = $menuParent[1]['link_title'];
print $menuParent;
}
?>
工作正常,但我发现没有二级导航的页面出现错误:
错误:注意:包含中未定义的偏移量:2
我原以为我的状况可以解决这个问题,但不起作用 您正在检查$menuParent[2],然后使用$menuParent[1]。可能检查$menuParent[1]:
PHP的数组是零索引的,因此插槽1是第二个插槽。使用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;
}
?>
如果要获取菜单路径的根,它应该位于$menuParent[1]中$menuParent[0]将始终返回首页,因此在尝试访问该页上的menuParent[1]之前,请确保检查是否设置了$menuParent[1]
<?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;
}
?>