您需要编写一个自定义walker扩展Walker_Nav_Menu
, 或多或少是这样的:
class My_Custom_Nav_Walker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "\\n<ul class=\\"dropdown-menu\\">\\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$item_html = \'\';
parent::start_el($item_html, $item, $depth, $args);
if ( $item->is_dropdown && $depth === 0 ) {
$item_html = str_replace( \'<a\', \'<a class="dropdown-toggle" data-toggle="dropdown"\', $item_html );
$item_html = str_replace( \'</a>\', \' <b class="caret"></b></a>\', $item_html );
}
$output .= $item_html;
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
if ( $element->current )
$element->classes[] = \'active\';
$element->is_dropdown = !empty( $children_elements[$element->ID] );
if ( $element->is_dropdown ) {
if ( $depth === 0 ) {
$element->classes[] = \'dropdown\';
} elseif ( $depth === 1 ) {
// Extra level of dropdown menu,
// as seen in http://twitter.github.com/bootstrap/components.html#dropdowns
$element->classes[] = \'dropdown-submenu\';
}
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
但不确定第3点和第4点需要什么。