php - Different menus for different user roles -
i'm trying display 2 different menus depending on user role.
i have setup new role called 'clients' using code (in functions.php):
add_role( 'client', 'client', array( 'read' => true, ) );
and have 2 different menus using 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 code i'm trying use call either client navigation or staff navigation (in header.php):
<?php if (current_user_can('client')){ //menu client role wp_nav_menu( array('theme-location' => 'client-navigation' )); }else{ //default menu wp_nav_menu( array('theme-location' => 'staff-navigation' )); } ?>
i've tried adding 'echo' before wp_nav_menu , changing theme-location
menu
, used menu name, shows staff-navigation
menu.
to use current_user_can
, should add own custom capability.
without doing that, you're looking role, following function, adapted from here job:
function check_for_clients() { global $current_user; $user_roles = $current_user->roles; $user_role = array_shift($user_roles); return ($user_role == 'client'); }
and in header.php file (note have 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' )); }
Comments
Post a Comment