仅当用户登录时才显示菜单项(而不是Word按登录)

时间:2016-08-16 作者:user101081

我正在开发一个web应用程序word press。我对文字出版社不熟悉,所以也许你会觉得这个问题很傻,我什么都试过了,但是i don\'t want to use any plugin.

以下是我需要向未登录用户显示的注册/登录菜单项,以及向登录用户显示的配置文件/注销菜单项。但在我的网站中,我没有使用word press登录函数wp\\u login(),我使用web服务获取登录响应,然后使用会话控制用户数据并访问不同页面上的用户数据。

在我完成的个人资料页面上

if(!isset($_SESSION[\'user\'])){
     header("Location:http://localhost:8080/wordpress/login/");
}
else {
    //page content
}
我试过这个,但不起作用

function my_wp_nav_menu_args( $args = \'\' ) {

if( isset($_SESSION[\'user\']) ) { 
$args[\'menu\'] = \'logged-in\';
} 
return $args;
}
add_filter( \'wp_nav_menu_args\', \'my_wp_nav_menu_args\' );
但是我怎样才能用菜单项做到这一点呢??

1 个回复
SO网友:Jacob Thygesen

使用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