【问题标题】:Drupal template, verify that the page is a menu itemDrupal模板,验证页面是菜单项
【发布时间】:2014-04-07 05:54:31
【问题描述】:

你好!

我想检查实际页面是否在菜单中。 我想做drupal模板(page.tpl.php)。 例如:

if ($page_is_a_menuitem):
    echo "This page is in the menu";
else:
    echo "This page not in the menu";
endif;

因为如果页面在菜单中a想要突出显示标题。

对不起,我的英语不好。

【问题讨论】:

  • 我认为您不需要手动执行此操作,因为如果您在页面上,Drupal 会在菜单项上为您提供“活动”类。
  • 尝试新代码。我希望旧代码更好并且有用

标签: php templates drupal menu drupal-7


【解决方案1】:

试试这个

这是返回到所有菜单列表:- menu_get_menus(true)

喜欢这个

Array
(
    [menu-footer-menu] => Footer Menu
    [main-menu] => Main menu
    [management] => Management
    [navigation] => Navigation
    [user-menu] => User menu
)

并且您想在所有列表中获取主菜单以使用它

$tree = menu_tree_all_data('main-menu')

这是在所有项目的主菜单中返回

否则你使用这个代码

$path = current_path();
$selected_menu= '';
$menu = menu_link_get_preferred($path = NULL, $selected_menu = NULL);

$menu 是在你想要之后返回当前页面的手册详细信息

echo "this menu is ".$menu['menu_name'] .' and menu title is '.$menu['title'];

【讨论】:

  • 这段代码不错,但我找到了更好的解决方案。最终代码在这里:$menu = menu_navigation_links('main-menu'); function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } echo in_array_r(current_path(), $menu) ? 'found' : 'not found';这个在多维菜单数组中搜索当前页面。
  • 此代码将扫描所有菜单:<?php $menu = array(); $menuLinks = array(); $menus = menu_get_menus($all = TRUE); foreach ($menus as $key => $value) { array_push($menu, menu_navigation_links($key)); } foreach ($menu as $key => $value) { foreach ($value as $key => $value2) { array_push($menuLinks, $value2['href']); } } print current_path(); echo (in_array(current_path(), $menuLinks)) ? '<br>found<br>' : '<br>not found<br>'; ?>
【解决方案2】:

获取整个菜单树

$tree = menu_tree_page_data('primary-links');

如果节点 id 存在于菜单中,则检查页面 tpl...

【讨论】:

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