我正在使用wp_nav_menu()
使用自定义walker
.
<ul>
<li id="menu-item-799" class="menu-item ... custom-theme-class"><a href="#">custom content</a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
...
</ul>
现在我想更改li
输出取决于类名。我用的是这样的东西:function theme_function($item_output, $item, $depth, $args){
$output = \'\';
// Overview Menu
if ( in_array( \'custom-theme-class\', (array) $item->classes ) ) {
$item_output = \'<div class="navbar-item">\'. wp_kses( $item->title, wp_kses_allowed_html( \'post\' ) ) .\'</div>\';
}
return $item_output;
}
add_filter(\'walker_nav_menu_start_el\', \'theme_function\', 10, 4);
这只是改变<li ...><a href="#">custom content</a></li>
至<li ..."><div class="navbar-item">custom content</div></li>
而我希望删除li
还有标签。所以它变成了<div class="navbar-item">custom content</div> (without the li tags)
在我的自定义菜单中walker
我使用了以下方法,但不起作用。function start_el(&$output, $item, $depth = 0, $args = array() , $id = 0) {
...
if ( in_array( \'custom-theme-class\', (array) $item->classes ) ) {
$output .= \'\';
} else {
$output .= $indent . \'<li\' . $id . $value . $class_names . $data_dropdown . \'>\';
}
}
function end_el(&$output, $item, $depth = 0, $args = array()) {
$classes = empty($item->classes) ? array() : (array)$item->classes;
$has_children = in_array(\'menu-item-has-children\', $classes);
if ($has_children && $depth == 0) {
$output.= "</div>\\n";
}
if ( in_array( \'custom-theme-class\', (array) $item->classes ) ) {
$output .= \'\';
} else {
$output.= "</li>\\n";
}
}
Theli
当菜单与一起使用时,标记仍然显示custom-theme-class
css类。我错过了什么?非常感谢您的帮助。