我已经将描述输入到wordpress中的父菜单项中,但它们没有显示在我的主题上.
我知道可以使用walker类来更改菜单,但我不知道如何编写它.
这是我想要实现的目标:
<nav id="main-menu" role="navigation"> <div class="menu-main-menu-container"> <ul id="menu-main-menu" class="menu"> <!-- REPEATING MENU ITEM START --> <li class="menu-parent-item"><a>Face</a> <ul class="sub-menu"> <li class="menu-image-container"> <div class="menu-image"></div> <div class="menu-description">[Face menu item description]</div> </li> <li class="heading">Face</li> <ul class="sub-menu"> <li class="menu-item">Sub menu 1</li> <li class="menu-item">Sub menu 2</li> <li class="menu-item">Sub menu 3</li> </ul> <li class="heading">Ear</li> <ul class="sub-menu"> <li class="menu-item">Sub menu 1</li> <li class="menu-item">Sub menu 2</li> <li class="menu-item">Sub menu 3</li> </ul> <li class="heading">Eyes</li> <ul class="sub-menu"> <li class="menu-item">Sub menu 1</li> <li class="menu-item">Sub menu 2</li> <li class="menu-item">Sub menu 3</li> </ul> </ul> </li> <!-- REPEATING MENU ITEM END --> </ul> </div>
如您所见,我只想显示父菜单项的描述,但它必须位于父列表项中的第一个ul.sub菜单中.
我怎么能编写一个使用start_lvl,start_el和end_lvl来有效处理这个问题的walker?
好吧,我不确定这是否是万无一失的,但这是我的尝试:
因此,您需要确保在管理菜单控制台中显示说明(在“屏幕选项”下拉列表中)
你的第一个菜单项应该是Face then Face再次作为它的孩子然后sub neu项目作为孩子.
希望这可以帮助!
在你的functions.PHP中
class Description_Walker extends Walker_Nav_Menu { /** * Start the element output. * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. May be used for padding. * @param array $args Additional strings. * @return void */ function display_element( $element,&$children_elements,$max_depth,$depth=0,$args,&$output ) { $id_field = $this->db_fields['id']; if ( is_object( $args[0] ) ) { $args[0]->has_children = ! empty( $children_elements[$element->$id_field] ); } return parent::display_element( $element,$children_elements,$depth,$output ); } function start_lvl( &$output,$args=array() ) { // depth dependent classes $indent = ( $depth > 0 ? str_repeat( "\t",$depth ) : '' ); // code indent $display_depth = ( $depth); // because it counts the first submenu as 0 $classes = array('sub-menu'); $class_names = implode( ' ',$classes ); // build html $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n"; } function start_el(&$output,$item,$depth = 0,$args = array(),$current_object_id = 0) { $classes = empty ( $item->classes ) ? array () : (array) $item->classes; $class_names = join( ' ',apply_filters( 'nav_menu_css_class',array_filter( $classes ),$item ) ); if ($args->has_children && $depth == 0){ ! empty ( $class_names ) and $class_names = ' class="menu-parent-item"'; }else if($depth == 1){ ! empty ( $class_names ) and $class_names = ' class="heading"'; }else{ ! empty ( $class_names ) and $class_names = ' class="'. esc_attr( $class_names ) .'"'; } $output .= "<li id='menu-item-$item->ID' $class_names>" ; $attributes = ''; ! empty( $item->attr_title ) and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"'; ! empty( $item->target ) and $attributes .= ' target="' . esc_attr( $item->target ) .'"'; ! empty( $item->xfn ) and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"'; ! empty( $item->url ) and $attributes .= ' href="' . esc_attr( $item->url ) .'"'; // insert description for top level elements only // you may change this $description = ( ! empty ( $item->description ) and 0 == $depth ) ? '<div class="menu-description">' . esc_attr( $item->description ) . '</div>' : ''; $title = apply_filters( 'the_title',$item->title,$item->ID ); if ( $depth == 0) {//top level items $item_output = $args->before.$title.'</li><ul class="sub-menu"><li class="menu-image-container"><div class="menu-image"></div>'.$description.'</li>'; } else if( $depth == 1){ $item_output = $args->before.$title.'</li>'; } else{//everything else $item_output = $args->before . "<a $attributes>" . $args->link_before . $title . '</a> ' . $args->link_after . $args->after; } // Since $output is called by reference we don't need to return anything. $output .= apply_filters( 'walker_nav_menu_start_el',$item_output,$args ); } }
<nav id="main-menu" role="navigation"><?PHP wp_nav_menu( array('menu' => 'Main','container' => 'div','container_class' => 'menu-main-menu-container','menu_id' => 'menu-main-menu','walker' => new Description_Walker )); ?></nav>