我正在尝试将子菜单项添加到父 li 的 div 内部。
这是我得到的...
<nav class="menu-main-menu-container">
<ul id="nav" class="">
<li id="menu-item-26" class="visit menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-24 current_page_item">
<div>
<a title="Locations | Traditions" href="#">Visit</a>
<span>Locations | Traditions</span>
<div class="arrow"></div>
</div>
<div class="main_sub_nav_wrap">
<h2>Visit</h2>
</div>
<ul class="sub-menu">
<li id="menu-item-82" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 1</a>
</li>
<li id="menu-item-83" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 2</a>
</li>
<li id="menu-item-84" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 3</a>
</li>
</ul>
</li>
</ul>
我想要的是让它像这样放入“main_sub_nav_wrap”。
<nav class="menu-main-menu-container">
<ul id="nav" class="">
<li id="menu-item-26" class="visit menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-24 current_page_item">
<div>
<a title="Locations | Traditions" href="#">Visit</a>
<span>Locations | Traditions</span>
<div class="arrow"></div>
</div>
<div class="main_sub_nav_wrap">
<h2>Visit</h2>
<ul class="sub-menu">
<li id="menu-item-82" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 1</a>
</li>
<li id="menu-item-83" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 2</a>
</li>
<li id="menu-item-84" class="menu-item menu-item-type-custom menu-item-object-custom">
<a href="#">Sub Link 3</a>
</li>
</ul>
</div>
</li>
</ul>
这是我通过扩展 walker 类来制作导航的内容......
<?php
class My_Walker 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 ) .'"' : '';
$item_output = $args->before;
$item_output .= '<div><a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a><span>' . $item->attr_title . '</span><div class="arrow"></div></div><div class="main_sub_nav_wrap"><div class="main_sub_nav"><div class="container"><div class="main_sub_nav_text"><h2>' . $item->title . '</h2></div></div></div></div>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
?>
我用它来添加到我的模板中......
<?php
$walker = new My_Walker;
wp_nav_menu(array(
'echo' => true,
'container' => '',
'theme_location' => 'header_menu',
'container' => 'nav',
'menu_class' => '',
'menu_id' => 'nav',
'depth' => 0,
'walker' => $walker
));
?>