Sometimes we want to show different menus to logged in users than logged out users. Especially it could be useful when we have for instance a e-commerce shop. So how to do it?
Step 1 - create menu for logged in users.
Notice the menu ID - it will be required later. So menu for logged users should have a "LOGOUT" menu item.
Step 2 - create menu for non-logged in users:
Step 3
Open functions.php
file which is in your theme directory and paste this code:
// Different menus to logged in users | |
function md_nav_menu_args( $args = '' ) { | |
if( is_user_logged_in() ) { | |
// Menu for logged users | |
$args['menu'] = 22; | |
} else { | |
// Menu for non-logged users | |
$args['menu'] = 23; | |
} | |
return $args; | |
} | |
add_filter( 'wp_nav_menu_args', 'md_nav_menu_args' ); |
That's all! Now you can see you have two different menus depended on you are logged in or not.
Top comments (0)