我可以通过调用自定义 menu_tree 函数来做到这一点:
print bootstrap_menu_tree('primary-links');
在我的主题的 template.php 中有这个:
function bootstrap_menu_tree($menu_name = 'navigation') {
static $menu_output = array();
if (!isset($menu_output[$menu_name])) {
$tree = menu_tree_page_data($menu_name);
$menu_output[$menu_name] = bootstrap_menu_tree_output($tree);
}
return $menu_output[$menu_name];
}
function bootstrap_menu_tree_output($tree, $dropdown_menu = false) {
$output = '';
$items = array();
// Pull out just the menu items we are going to render so that we
// get an accurate count for the first/last classes.
foreach ($tree as $data) {
if (!$data['link']['hidden']) {
$items[] = $data;
}
}
$num_items = count($items);
foreach ($items as $i => $data) {
$extra_class = array();
if ($i == 0) {
$extra_class[] = 'first';
}
if ($i == $num_items - 1) {
$extra_class[] = 'last';
}
$extra_class = implode(' ', $extra_class);
if ($data['below']) {
$link = theme('menu_item_link', $data['link'], true);
$output .= theme('menu_item', $link, $data['link']['has_children'], bootstrap_menu_tree_output($data['below'], true), $data['link']['in_active_trail'], $extra_class);
}
else {
$link = theme('menu_item_link', $data['link']);
$output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
}
}
return $output ? theme('menu_tree', $output, $dropdown_menu) : '';
}
// theme_menu_tree()
function phptemplate_menu_tree($tree, $dropdown_menu = false) {
$class = 'nav';
if($dropdown_menu) $class = 'dropdown-menu';
return '<ul class="'.$class.'">'. $tree .'</ul>';
}
// theme_menu_item()
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
$class = ($menu ? 'dropdown' : ($has_children ? 'dropdown' : ''));
if (!empty($extra_class)) {
$class .= ' '. $extra_class;
}
if ($in_active_trail) {
$class .= ' active-trail';
}
return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
}
function phptemplate_menu_item_link($link, $dropdown_toggle = false) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
if($dropdown_toggle) {
// add class
if (isset($link['localized_options']['attributes']['class'])) {
$link['localized_options']['attributes']['class'] .= ' dropdown-toggle';
}
else {
$link['localized_options']['attributes']['class'] = 'dropdown-toggle';
}
// add toggle
$link['localized_options']['attributes']['data-toggle'] = 'dropdown';
// add carat
$link['title'] .= '<b class="caret"></b>';
$link['localized_options']['html'] = true;
}
return l($link['title'], $link['href'], $link['localized_options']);
}