If you’re using WordPress as a membership site, you may want to display a special navigation menu to logged in members.
There’s different ways to do this using either CSS classes or PHP code.
Custom functions are the best way to go in my opinion because you don’t have to worry about adding more CSS to your child themes files and slowing down your site. PHP code is more powerful and efficient. You also don’t need to worry about cross browser issues.
To display a different nav menu for logged in and logged out users, you firstly need to create 2 menu’s.
I won’t show you how to do that because its so easy even your mum could do it!
Create 2 nav menus
- One for Logged in members
- And one for logged out visitors
Here’s what they look like once you’ve created them.
You can then select the non-members menu as the primary menu and leave the members menu unassigned.
You’d probably want to include a registration link in the non members nav menu but not in the members menu.
Paste Code In Functions File
This is the code you need to paste into your child themes functions.php file.
Here’s some code which you can use to display the nav menu for logged in members in a different location like the footer for either the primary or secondary nav menu’s.
Nav Menu Roles Plugin
Here’s a plugin which pretty much does the same thing as the code snippets above.
This plugin enables you to hide menu items based on user roles. You can:
- Limit display of this menu item to users who are logged in or out, or customize by role.
- Limit display of this menu item to users of selected roles.
Nav Menu Items Visibility Plugin
This plugin enables you to add conditional tags to each nav menu item so you can hide or display each menu link based on specific conditions.
The conditional tag to hide menu items for logged in users is:
That’s all folks!
I know there are other ways to display different menus for logged in and logged out users however these are the best in my opinion.
How about you? Do you have a better solution than this one?
Surendra Soni says
How can I implement the same code with custom menu location like we use Top Header Area to show our custom Menu.
This looks just like the sort of thing I’m trying to do, but I would like to change the top menu of my site rather than the main nav menu. I’m kinda new to all this, and was wondering how I could alter your code to keep the main nav the same but change it up top?
Thanks in advance.
Brad Dalton says
You’ll need to use both snippets and make sure the menu locations in the code and settings are correct otherwise it will not work.
Your code for the 2 nav menus is excellent.
Anyway my failed approach to this was to using two different menus by registering two nav menu-locations, logged-in and logged-out. Also I’m assigning a menu to each of them. In my functions.php I’m declaring a function like ‘display_main_menu(…)’, which calls wp-nav-menu with either the first, or the second menu-location as an argument, depending on login-status. But i couldn’t get this method to work.
I don’t like the plugin so i’m going to use your code instead.
Brad Dalton says
The code works perfectly and has been tested. The reason it might not work for you is it includes 2 code snippets in the one paste. The second one is for use if you have a menu in a different location. Otherwise, use the first block of code.
Did you read this: You only need the first code snippet from this block if you are using the same menu location for logged in members and logged out non members. Example: The primary menu.
If you are using both the primary and secondary nav menu, then you should use the second block of code.
I included both options because some people like to use use a different menu location for logged in members and logged out visitors.
You’re right, didn’t see that line.
Brad Dalton says
Yep. Most people will use the first block when using the same menu location for logged in and logged out users. Otherwise you’ll need the first 2 blocks of of code if you want the menu to display in different locations for logged in and logged out users.