我的网站上有以下二级导航菜单,它是使用本机的WordPress外观>菜单功能构建的:

在我的functions.php 根据用户的访问权限,删除和添加“登录/注册”和“注销”超链接的文件:
// Add a Login hyperlink to the secondary navigation menu if the user is logged-out
function wpa_remove_menu_item( $items, $menu, $args ) {
if( is_admin() || ! is_user_logged_in() ) return $items;
foreach ( $items as $key => $item ) {
if ( \'Login / Register\' == $item->title ) unset( $items[$key] );
}
return $items;
}
add_filter( \'wp_get_nav_menu_items\', \'wpa_remove_menu_item\', 10, 3 );
// Remove the Logout hyperlink from the secondary navigation menu when the user is logged-in
function wpa_add_menu_item( $items, $menu, $args ) {
if( is_user_logged_in() ) return $items;
foreach ( $items as $key => $item ) {
if ( \'Logout\' == $item->title ) unset( $items[$key] );
}
return $items;
}
add_filter( \'wp_get_nav_menu_items\', \'wpa_add_menu_item\', 10, 3 );
输出如下:

“注销”菜单项正在使用以下超链接:

如何替换此URL,以便当用户在辅助导航菜单中选择注销项目时,不会将其重定向到确认屏幕。。。

并且仍然位于同一层次结构中(位于“次要标题”的“我的帐户”父菜单中,而不是位于整个菜单的开头或结尾)?

非常感谢。
最合适的回答,由SO网友:Rajeev Vyas 整理而成
未测试,但可能有效,请使用wp_logout_url()
wordpress内置函数以获取注销url。
// Add a Login hyperlink to the secondary navigation menu if the user is logged-out
function wpa_remove_menu_item( $items, $menu, $args ) {
if( is_admin() || ! is_user_logged_in() ) return $items;
foreach ( $items as $key => $item ) {
if ( \'Login / Register\' == $item->title ) unset( $items[$key] );
if ( \'Logout\' == $item->title ){$items[$key]->url=wp_logout_url();}
}
return $items;
}
add_filter( \'wp_get_nav_menu_items\', \'wpa_remove_menu_item\', 10, 3 );