我在尝试使用下拉菜单时遇到问题。这是非wordpress菜单:
<ul class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" >Dropdowns</a>
<ul class="dropdown-menu">
<li><a href="#">Custom Menu</a></li>
<li><a href="#">Custom Menu</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" >Sub Menu</a>
<ul class="dropdown-menu">
<li><a href="#">Custom Menu</a></li>
<li><a href="#">Custom Menu</a></li>
</ul>
</li>
</ul>
</li>
</ul>
我尝试将其转换为wordpress菜单,如下所示:
<div class="collapse navbar-collapse" id="navbar-menu">
<?php
wp_nav_menu( array(
\'menu_class\' => \'nav navbar-nav data-in="fadeInDown" data-out="fadeOutUp"\',
\'container\' => \'\',
\'depth\' => 2,
\'theme_location\' => \'main-menu\',
\'walker\' => new My_BS_Walker_Nav_Menu(),
));
?>
</div>
这是沃克密码,但我只是把自己弄糊涂了:
<?php
if ( ! defined( \'ABSPATH\' ) ) {
exit; // Exit if accessed directly.
}
/**
* Custom walker for WordPress custom menus.
*
* @link https://wordpress.stackexchange.com/q/298663/137402
*
* @see Walker_Nav_Menu
*/
class My_BS_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* @see Walker_Nav_Menu::start_lvl()
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
// Default class.
$classes = array( \'dropdown-menu\' );
/**
* Filters the CSS class(es) applied to a menu list element.
*/
$class_names = join( \' \', apply_filters( \'nav_menu_submenu_css_class\', $classes, $args, $depth ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$output .= \'\' .
"<ul{$class_names}>";
}
/**
* Ends the list of after the elements are added.
*
* @see Walker_Nav_Menu::end_lvl()
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$output .= \'\' .
\'</ul>\'; // End .menu-vertical
}
/**
* Starts the element output.
*
* @see Walker_Nav_Menu::start_el()
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$item->item_spacing = \'discard\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
if ( in_array( \'menu-item-has-children\', $classes ) ) {
$depth2 = intval( $args->depth );
// Displays a link to the page and not a "dropdown trigger" text.
if ( $depth2 > 0 && (int) $depth + 1 === $depth2 ) {
return parent::start_el( $output, $item, $depth, $args, $id );
}
} else {
// Displays a link to the page and not a "dropdown trigger" text.
return parent::start_el( $output, $item, $depth, $args, $id );
}
$classes[] = \'dropdown\';
$classes[] = \'menu-item-\' . $item->ID;
/**
* Filters the CSS class(es) applied to a menu item\'s list item element.
*/
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$output .= \'<li\' . $class_names .\'>\';
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( \'the_title\', $item->title, $item->ID );
/**
* Filters a menu item\'s title.
*/
$title = apply_filters( \'nav_menu_item_title\', $title, $item, $args, $depth );
$item_output = \'<a href="#" class="dropdown-toggle" data-toggle="dropdown" >\';
$item_output .= $title;
$item_output .= \'</a>\';
$output .= $item_output;
}
/**
* Ends the element output, if needed.
*
* @see Walker_Nav_Menu::end_el()
*/
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= \'</li>\';
}
} // My_BS_Walker_Nav_Menu
最合适的回答,由SO网友:Sally CJ 整理而成
walker代码是可以的,但为了获得此标记:
<ul class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">
...
</ul>
。。在
array
传递给
wp_nav_menu()
功能,设置
items_wrap
收件人:
<ul id="%1$s" class="%2$s" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>
因此,完整的标记将是:
<div class="collapse navbar-collapse" id="navbar-menu">
<?php
wp_nav_menu( array(
\'menu_class\' => \'nav navbar-nav\',
\'container\' => \'\',
\'depth\' => 2,
\'theme_location\' => \'main-menu\',
\'walker\' => new My_BS_Walker_Nav_Menu(),
\'items_wrap\' => \'<ul id="%1$s" class="%2$s" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>\',
));
?>
</div>
或可以省略
menu_class
参数,只需通过
items_wrap
值:
<div class="collapse navbar-collapse" id="navbar-menu">
<?php
wp_nav_menu( array(
\'container\' => \'\',
\'depth\' => 2,
\'theme_location\' => \'main-menu\',
\'walker\' => new My_BS_Walker_Nav_Menu(),
\'items_wrap\' => \'<ul id="%1$s" class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>\',
));
?>
</div>