Discover in this tutorial how to display a different menu depending on whether the user is logged in or not in WordPress, using code.
Being able to show or hide menu items depending on the user’s status is very handy, especially for membership sites.
In fact, if you access OsomCode without logging in you will see a menu:
And when you have logged in with your account you will see another one:
In this case I will show you how to load different menus, which is much more appropriate if the menu between logged in and not logged in is very different.
Here we go:
Snippet to display a different menu to logged in users in WordPress
First of all, you must create two menus, one that is the one you want to show to logged in users, and another one to non-logged in users.
Once you have it, add the following snippet to the functions.php file of your theme:
In this snippet you use the
wp_nav_menu_args filter in combination with the
is_user_logged_in conditional tag to display one menu or another depending on whether the user is logged in or not.
In this case, the menus are called “User logged in” and “User not logged in” but if you want you can use another nomenclature. If you do so, you will only have to replace it in the snippet.
The code works for both Genesis child themes and other WordPress themes.
You can see here how easy it is to load a specific menu depending on whether or not the user is logged into WordPress.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉