Drupal 7自定义菜单渲染

问题描述 投票:0回答:2

我是druapl主题开发的新手。从drupal管理员创建分层菜单。我想在page.tpl.php文件中呈现此菜单。我使用了以下代码,但它没有渲染子菜单。它并不表示它们显示为无,但它们(子菜单)根本没有呈现。

$params = array(
  'links' => menu_navigation_links('menu-eschopper-main-menu'),
  'attributes' => array(
    'class'=> array('nav','navbar-nav','collapse', 'navbar-collapse'),
  ),
);
print theme('links', $params);
drupal drupal-7 drupal-theming drupal-navigation
2个回答
4
投票

我曾经手动完成,以完美控制渲染。您可以使用menu_tree_all_data()加载菜单链接,并在其上使用foreach:

的template.php

function render_menu_tree($menu_tree) {
    print '<ul>';
    foreach ($menu_tree as $link) {
        print '<li>';
        $link_path = '#';
        $link_title = $link['link']['link_title'];
        if($link['link']['link_path']) {
            $link_path = drupal_get_path_alias($link['link']['link_path']);
        }
        print '<a href="/' . $link_path . '">' . $link_title . '</a>';
        if(count($link['below']) > 0) {
            render_menu_tree($link['below']);
        }
        print '</li>';
    }
    print '</ul>';
}

page.tpl.php中

$main_menu_tree = menu_tree_all_data('menu-name', null, 3);
render_menu_tree($main_menu_tree);

如果您不想使用深度限制,请使用menu_tree_all_data('menu-name')


0
投票

您使用'menu_navigation_links'的功能仅显示单个级别的链接。您可以更好地查看菜单块模块(https://www.drupal.org/project/menu_block)或使用以下示例等功能:

/**
 * Get a menu tree from a given parent.
 *
 * @param string $path
 *   The path of the parent item. Defaults to the current path.
 * @param int $depth
 *   The depth from the menu to get. Defaults to 1.
 *
 * @return array
 *   A renderable menu tree.
 */
function _example_landing_get_menu($path = NULL, $depth = 1) {
  $parent = menu_link_get_preferred($path);
  if (!$parent) {
    return array();
  }

  $parameters = array(
    'active_trail' => array($parent['plid']),
    'only_active_trail' => FALSE,
    'min_depth' => $parent['depth'] + $depth,
    'max_depth' => $parent['depth'] + $depth,
    'conditions' => array('plid' => $parent['mlid']),
  );

  return menu_build_tree($parent['menu_name'], $parameters);
}

此函数将返回从给定URL开始的给定深度的菜单树。

以下代码将生成一个子菜单,其中当前页面为父级,并显示深度为2的子项。

$menu = _example_landing_get_menu(NULL, 2);
print render($menu);
© www.soinside.com 2019 - 2024. All rights reserved.