我尝试了下面的代码,它成功了。
将此添加到functions.php
register_nav_menus(array(
\'top-menu\' => __(\'Menu1\', \'twentyfourteen\'),
\'side-menu\' => __(\'Menu2\', \'twentyfourteen\'),
\'footer-menu\' => __(\'Menu3\', \'twentyfourteen\')
) );
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
$menu_locations = get_nav_menu_locations();
if ( has_term($menu_locations[\'top-menu\'], \'nav_menu\', $item) ) {
$item_output = preg_replace(\'/<a /\', \'<a class="list-group" \', $item_output, 1);
}
return $item_output;
}
add_filter(\'walker_nav_menu_start_el\', \'my_walker_nav_menu_start_el\', 10, 4);
最后,您必须为特定菜单选择“Menu1”选项,您必须在该菜单上从仪表板添加锚定自定义类
Apperance->menus
. (选择
menu2
或
menu3
对于锚定链接不需要自定义类的其他菜单)
要将“活动类”添加到特定菜单的第一个菜单项,请尝试以下操作:
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
$menu_locations = get_nav_menu_locations();
if ( has_term($menu_locations[\'top-menu\'], \'nav_menu\', $item) ) {
$item_output = preg_replace(\'/<a /\', \'<a class="list-group" \', $item_output, 1);
if ($item->menu_order == 1){
$item_output = preg_replace(\'/<a /\', \'<a class="list-group active" \', $item_output, 1);
}
}
return $item_output;
}
add_filter(\'walker_nav_menu_start_el\', \'my_walker_nav_menu_start_el\', 10, 4);
如果必须将活动类添加到所有菜单的第一个菜单项中,请使用以下选项:
function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
$menu_locations = get_nav_menu_locations();
if ( has_term($menu_locations[\'top-menu\'], \'nav_menu\', $item) ) {
$item_output = preg_replace(\'/<a /\', \'<a class="list-group" \', $item_output, 1);
}
if ($item->menu_order == 1){
$item_output = preg_replace(\'/<a /\', \'<a class="active" \', $item_output, 1);
}
return $item_output;
}
add_filter(\'walker_nav_menu_start_el\', \'my_walker_nav_menu_start_el\', 10, 4);