简体   繁体   English

使用 nav_menu_item_id 添加到 Wordpress 菜单 ID

[英]adding to Wordpress menu ID with nav_menu_item_id

I have two nav menus (topbar and main) and I want to add text to the li ID of the topbar menu.我有两个导航菜单(顶栏和主菜单),我想将文本添加到顶栏菜单的 li ID。 How can I prepend text "topbar-" to the li ID, without affecting the main nav menu or any footer menu?如何在不影响主导航菜单或任何页脚菜单的情况下将文本“topbar-”添加到 li ID?

add_filter('nav_menu_item_id', 'topbar_menu_id', 10, 3);
function topbar_menu_id( $menu_id, $item, $args ) {
    if (!is_admin() && $args->theme_location == 'topbar_menu') {
        $id = apply_filters( 'nav_menu_item_id', 'topbar-menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';
    }
}

Thanks in advance提前致谢

If topbar_menu is a menu name, then use this code如果 topbar_menu 是菜单名称,则使用此代码

 add_filter('nav_menu_item_id', 'topbar_menu_id', 10, 3);
 function topbar_menu_id( $menu_id, $item, $args ) {
     if (!is_admin() && $args->menu->name == 'topbar_menu') {
             $menu_id='topbar-'.$menu_id;
     }
         return $menu_id;
 }

else if topbar_menu is a location name, then use this code否则,如果 topbar_menu 是位置名称,则使用此代码

add_filter('nav_menu_item_id', 'topbar_menu_id', 10, 3);
 function topbar_menu_id( $menu_id, $item, $args ) {
     if (!is_admin() && $args->theme_location == 'topbar_menu') {
             $menu_id='topbar-'.$menu_id;
     }
         return $menu_id;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM