这实际上创建了两个实例,其中在主要和次要级别的标记中显示了一个span类,因此您可以为span类添加不同的图像,以用于设计和导航目的。
这有助于用户和开发人员显示标题菜单是否有下拉菜单,并使其更容易在网站中导航。
1. Add this code below to your functions.php first.
class Nav_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = \'menu-item-\' . $item->ID;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
$id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . \'<li\' . $id . $class_names .\'>\';
$attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
$attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
$attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
$attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\';
$item_output = $args->before;
$item_output .= \'<a\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
if ( \'primary\' == $args->theme_location ) {
$submenus = 0 == $depth || 1 == $depth ? get_posts( array( \'post_type\' => \'nav_menu_item\', \'numberposts\' => 1, \'meta_query\' => array( array( \'key\' => \'_menu_item_menu_item_parent\', \'value\' => $item->ID, \'fields\' => \'ids\' ) ) ) ) : false;
$item_output .= ! empty( $submenus ) ? ( 0 == $depth ? \'<span class="arrow"></span>\' : \'<span class="sub-arrow"></span>\' ) : \'\';
}
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
2. Add code below to your header.php where your wp_nav_menu is installed. 下面解释的是代码,因此它安装了新的自定义菜单,在这种情况下将是Nav\\u Walker\\u Nav\\u menu。
<?php wp_nav_menu( array( \'container_class\' => \'menu-header\', \'theme_location\' => \'primary\', \'walker\' => new Nav_Walker_Nav_Menu() ) ); ?>
3. Add some CSS
因此,它能够在主要和次要级别的标记中显示新的跨距箭头图像。
#menu-header-menu li span.arrow { height:12px;background-image: url("images/nav-arrows.png");background-position: -8px -3px;background-repeat: no-repeat;float: right;margin: 0 0px 0 10px;text-indent: -9999px;width: 12px;}
#menu-header-menu li a:hover span.arrow{ height:12px;background-image: url("images/nav-arrows.png");background-position: -39px -3px;background-repeat: no-repeat;float: right;margin: 0 0px 0 10px;text-indent: -9999px;width: 12px;}
#menu-header-menu ul.sub-menu li span.sub-arrow { height:12px;background-image: url("images/nav-arrows.png");background-position: -8px -39px;background-repeat: no-repeat;float: right;margin: -2px 0px 0 10px;text-indent: -9999px;width: 12px;}
#menu-header-menu ul.sub-menu li a:hover span.sub-arrow{ height:12px;background-image: url("images/nav-arrows.png");background-position: -39px -39px;background-repeat: no-repeat;float: right;margin: -2px 0px 0 10px;text-indent: -9999px;width: 12px;}
希望对您有所帮助!:)