使用wp_nav_menu_objects 筛选以访问菜单项
我使用了以下代码:
add_filter(\'wp_nav_menu_objects\', \'ad_filter_menu\', 10, 2);
function ad_filter_menu($sorted_menu_objects, $args) {
// check for the right menu to rename the menu item from
// here we check for theme location of \'primary-menu\'
// alternatively you can check for menu name ($args->menu == \'menu_name\')
if ($args->theme_location != \'primary\')
return $sorted_menu_objects;
// rename the menu item that has a title of \'Log ind\'
if (is_user_logged_in()) {
foreach ($sorted_menu_objects as $key => $menu_object) {
// can also check for $menu_object->url for example
// see all properties to test against:
// print_r($menu_object); die();
if ($menu_object->title == \'Log in\') {
$current_user = wp_get_current_user();
$menu_object->title = $current_user->user_login . " - Log out";
$menu_object->url = wp_logout_url();;
}
}
}
return $sorted_menu_objects;
}
在这里找到了基础:
Remove a menu item in menu显然你需要改变路线
if (is_user_logged_in()) {
至
if( isset($_SESSION[\'user\']) ) {
您可能需要检查其他菜单项,以根据状态(登录或注销)更改其标题和url