您可以使用walker_nav_menu_start_el
过滤器:
add_filter( \'walker_nav_menu_start_el\', \'wpse_add_arrow\',10,4);
function wpse_add_arrow( $item_output, $item, $depth, $args ){
//Only add class to \'top level\' items on the \'primary\' menu.
if(\'primary\' == $args->theme_location && $depth ==0){
$item_output .=\'<span class="arrow"></span>\';
}
return $item_output;
}
这假设您正在使用
wp_nav_menu( array( \'theme_location\' => \'primary\') );
显示菜单。
更新了回复意见的答案。添加span
仅将类设置为具有子元素的顶级元素,您需要使用自定义walker:
class SH_Arrow_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("\\t", $depth);
if(\'primary\' == $args->theme_location && $depth ==0){
$output .=\'<span class="arrow"></span>\';
}
$output .= "\\n$indent<ul class=\\"sub-menu\\">\\n";
}
}
要使用此选项,您需要设置
walker
中的参数
wp_nav_menu
:
wp_nav_menu( array(
\'theme_location\' => \'primary\',
\'walker\'=> new SH_Arrow_Walker_Nav_Menu()
) );