我用这个助行器在functions.php
class main_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 = \'\';
$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 ) . \' nav-item"\';
$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;
if ( $args->walker->has_children && 0 === $depth ) {
$item_output .= \'<span class="nav-link">\';
} else {
$item_output .= \'<a class="nav-link" \' . $attributes . \'>\';
}
$item_output .= $args->link_before .apply_filters( \'the_title\', $item->title, $item->ID );
$item_output .= ( $args->walker->has_children && 0 === $depth ) ? \' </span>\' : \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
获取
this structure menu:
<ul class="nav navbar-nav nav__top-menu">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<span class="nav-link">About</span>
<ul class="sub-menu">
<li class="nav-item">
<a class="nav-link" href="#">text 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">text 2</a>
</li>
</ul>
</li>
</ul>
如果父项具有子菜单-正在成为
span
如何制作-单击父项并转到第一个子项?
Update
我找到了这个
plugin. 首先发布的搜索子页
function has_published_children
. 在我的菜单中,页面的位置可能会改变。需要的功能,搜索页面不是按发布日期,而是按位置在菜单中。