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, $args){
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
$class_names = \' class="\'. esc_attr( $class_names ) . \'"\';
$output .= $indent . \'<li id="menu-item-\'. $item->ID . \'"\' . $value . $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 ) .\'"\' : \'\';
$description = ! empty( $item->description ) ? \'<span>\'.esc_attr( $item->description ).\'</span>\' : \'\';
$item_output = $args->before;
$item_output .= \'<a\'. $attributes .\'><span data-hover="\'. $item->title .\'">\';
$item_output .=$args->link_before .apply_filters( \'the_title\', $item->title, $item->ID );
$item_output .= $description.$args->link_after;
$item_output .= \'</span></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_Walker_Nav_Menu
.
<?php wp_nav_menu( array( \'container_class\' => \'menu-header\', \'theme_location\' => \'primary\', \'walker\' => new Nav_Walker_Nav_Menu() ) ); ?>
希望这对你有帮助!
解决方案2:使用wp_list_pages
请退房
this page . 您可以看到他们的代码片段。如果在链接标记周围放置跨距,则可以使用
link_before
和
link_after
wp_list_pages("link_before=<span data-hover="link-text-here">&link_after=</span>");