How to Show Different Menus to Logged in WordPress Users
In WordPress, there is an option to create multiple menus but no condition to make it visible only for logged in users or guest. Users might need this option to show different menus to logged in WordPress users for various scenarios like “login menu and guest post items should visible only for guest” and “logout button should visible only for logged in users”.
See Also: 10+ Most Common WordPress Mistakes to Avoid
Create Two Menus – (if not created it already)
- Login to WordPress admin dashboard.
- Navigate to Appearance Then Menus.
- Click create new menu.
- Create two menus. I have created two menus with name guest and logged-in.
See Also: Top 16 Best Free Essential WordPress Plugins
Using code – Show Different Menus to Logged In WordPress Users
Add the below lines to the functions.php file in the theme folder.
// Show different menus to logged in users and guest function ett_diff_menu( $args = '') { if( is_user_logged_in()) { $args = 'logged-in'; } else { $args['menu'] = 'guest'; } return $args; } add_filter( 'wp_nav_menu_args', 'ett_diff_menu' );
Modify logged-in and guest with your menu name. Save the file. Now you can see logged-in menu will appear only to logged in users and guest menu will appear only to guests.
See Also: How to make WordPress site Load Faster
If your theme has eligible to display more than one menu
The above method will work only if your theme has one menu location. If it has more than one menu to display like header menu, main menu, footer menu, social menu etc, then the above method will display the same logged-in and guest menus in all the menu location overriding the settings in Menus panel. So apply the below method if your theme contains more than one menu location.
Add the below lines to the functions.php file in the theme folder.
// Show different menus to logged in users and guest function ett_diff_menu( $args = '') { if ($args['theme_location'] == 'header-menu') { if( is_user_logged_in()) { $args['menu'] = 'category'; } else { $args['menu'] = 'category-join'; } } return $args; } add_filter( 'wp_nav_menu_args', 'ett_diff_menu' );
Modify the header-menu to your menu location name. You can find your menu location name in the theme’s header file or from your theme provider. Modify logged-in and guest with your menu name. Save the file. Now you can see the logged-in menu and guest menu will appear only in the header menu and remaining menu locations will be unaffected.
See Also: Prevent WordPress Content Theft – text and images
Using Plugin – Show Different Menus to Logged In WordPress Users
Above methods are applicable only to advanced users. Basic users can easily accomplish the same using any of the below plugin instead of editing the functions.php file.
helpful…
Very clear instruction which I exactly need. I done using the 1st method. Great man, Keep going….
works perfectly. thanks