【问题标题】:How to display parent menu item in node template? Drupal 7如何在节点模板中显示父菜单项?德鲁巴 7
【发布时间】:2011-08-13 22:56:54
【问题描述】:

如何在节点模板中显示父菜单项?

我想与当前页面一起显示父菜单项;但我不需要别人。

编辑:我启用了菜单面包屑模块​​并添加了以下代码:

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

它工作正常,但对于没有二级导航的页面出现错误: 错误:注意:未定义的偏移量:include() 中的 2

我认为我的条件 sizeof 可以解决问题,但无法正常工作。

【问题讨论】:

    标签: drupal-7


    【解决方案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] 是否已设置
    【解决方案2】:

    您正在检查$menuParent[2],但随后使用$menuParent[1]。也许检查$menuParent[1]

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

    PHP 的数组是零索引的,所以插槽 1 是第二个插槽。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多