Here’s how you can easily show different menus to logged-in users in WordPress using either a plugin or manually through code:
Method 1: Show Different Menus to Logged-in Users Using a Plugin
Using a plugin is the simpler method and doesn’t require any coding skills.
- Install and Activate the Conditional Menus Plugin:
- Install the “Conditional Menus” plugin from the WordPress plugin repository.
- Activate the plugin.
- Create Your Menus:
- Navigate to
Appearance » Menus
. - Create two menus: one for logged-in users and another for non-logged-in users. Customize them as per your requirements.
- Navigate to
- Assign Menus Using Conditional Menus Plugin:
- Go to
Appearance » Menus
. - Click on the
Manage Locations
tab. - Assign your menus to different locations based on user conditions:
- Click on
+ Conditional Menu
. - Select your logged-in user menu from the drop-down.
- Add a condition (
User logged in
) to specify when this menu should be displayed. - Save your settings.
- Click on
- Go to
- Preview:
- Visit your website to see how the menus change based on whether you are logged in or not.
Method 2: Manually Select Logged-in Menu Using Code
If you prefer to use code, follow these steps:
- Add Code Snippet:
- Access your WordPress theme’s
functions.php
file (or use a site-specific plugin for safer code management). - Add the following PHP code snippet:
phpfunction my_wp_nav_menu_args( $args = ” ) {
if( is_user_logged_in() ) {
// Logged in menu to display (replace 43 with your logged-in menu ID)
$args[‘menu’] = 43;
} else {
// Non-logged-in menu to display (replace 35 with your non-logged-in menu ID)
$args[‘menu’] = 35;
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ ); - Access your WordPress theme’s
-
-
- Replace
43
and35
with the IDs of the menus you created earlier. You can find the ID by editing the menu inAppearance » Menus
and checking the URL formenu=ID
.
- Replace
- Save Changes:
- Save the
functions.php
file or your site-specific plugin.
- Save the
- Preview:
- Visit your website to see how the menus change based on the logged-in status.
Conclusion
Both methods allow you to dynamically change navigation menus in WordPress based on whether users are logged in or not. The plugin method is easier for most users, while the code snippet method gives you more control and customization options directly through code. Choose the method that best fits your comfort level with WordPress and coding.
-