这是使用此代码的方法,它将显示父级描述。
使用步骤。
1:将下面的类复制到functions.php文件中。
2:调用菜单如下
wp_nav_menu(array(
'menu_id'=>'',
'menu_class'=>'',
'container'=>'',
'theme_location'=>'#enter theme location#',
'walker'=> new customize_menu_walker()
));
<br/>
3:查看结果。
class customize_menu_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
// $dbclasses=$item->classes;
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$dbclasses=$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $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;
//specially for two menu
//specially for two menu
//convert string to array
$dbclassesArr = explode(" ",$dbclasses);
$item_output .= '<a'. $attributes .'>';
if(in_array("menu-item-has-children", $dbclassesArr)){
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';//description display here.
}
$item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= $description.$args->link_after;
$item_output .= ' '.'</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
现在更新此功能后,您的菜单将显示父菜单描述。
这是代码。
感谢您。