不同用户角色的不同菜单

我试图根据用户角色显示两个不同的菜单。

我使用以下代码(在functions.php中)设置了一个名为”客户端”的新角色:

add_role( 'client', 'Client', array(
    'read' => true, ) 
);

使用此代码, 我有两个不同的菜单(在functions.php中):

function register_my_menus() {
  register_nav_menus(
    array(
  'client-navigation' => __( 'Client Navigation' ), 'staff-navigation' => __( 'Staff Navigation' ), )
  );
}
add_action( 'init', 'register_my_menus' );

这是我试图用来调用”客户端导航”或”人员导航”的代码(在header.php中):

<?php
        if (current_user_can('client')){
            //menu for client role
             wp_nav_menu( array('theme-location' => 'client-navigation' ));

        }else{
            //default menu
             wp_nav_menu( array('theme-location' => 'staff-navigation' ));
        }
?>

我还尝试过在wp_nav_menu之前添加” echo”并将主题位置更改为菜单, 并使用菜单名称, 但它始终显示人员导航菜单。


#1


要使用current_user_can, 你应该添加自己的自定义功能。

无需执行此操作, 而是在寻找角色时, 从此处改编的以下功能可以完成工作:

function check_for_clients() {
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    return ($user_role == 'client');
}

然后在你的header.php文件中(请注意, 你在theme_location中有一个错字):

if ( check_for_clients() ){
     wp_nav_menu( array('theme_location' => 'client-navigation' ));

} else {
     wp_nav_menu( array('theme_location' => 'staff-navigation' ));
}

来源:

https://www.srcmini02.com/67227.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_36939.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?