Reputation: 917
I'm trying to display two different menus depending on the user role.
I have setup a new role called 'clients' using this code (in functions.php):
add_role( 'client', 'Client', array(
'read' => true,
)
);
and I have two different menus using this code (in functions.php):
function register_my_menus() {
register_nav_menus(
array(
'client-navigation' => __( 'Client Navigation' ),
'staff-navigation' => __( 'Staff Navigation' ),
)
);
}
add_action( 'init', 'register_my_menus' );
here is the code which I'm trying to use to call either Client Navigation or Staff Navigation (in 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' ));
}
?>
I've also tried adding 'echo' before wp_nav_menu and changing theme-location
to menu
and used the menu name, but it always shows staff-navigation
menu.
Upvotes: 1
Views: 2223
Reputation: 26065
To use current_user_can
, you should add your own custom capability.
Without doing that, as you're looking for the role, the following function, adapted from here does the job:
function check_for_clients() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return ($user_role == 'client');
}
And then in your header.php file (note that you have a typo in theme_location
):
if ( check_for_clients() ){
wp_nav_menu( array('theme_location' => 'client-navigation' ));
} else {
wp_nav_menu( array('theme_location' => 'staff-navigation' ));
}
Upvotes: 2