I want to be able to hide the 'My account' button when users are logged out and I want to be able to hide the 'Registration' button when users are logged in.
How would I go about doing this? I'm still a amatuer at WordPress and I'm still learning, this is what I have so far in my nav-menus.php file.
if( is_user_logged_in() ) {
wp_nav_menu( array( 'My Account' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'Registration' => 'not-logged-users' ) );
}
I know this isn't correct, but I would love some help with it, please let me know if I'm missing any information.
I want to be able to hide the 'My account' button when users are logged out and I want to be able to hide the 'Registration' button when users are logged in.
How would I go about doing this? I'm still a amatuer at WordPress and I'm still learning, this is what I have so far in my nav-menus.php file.
if( is_user_logged_in() ) {
wp_nav_menu( array( 'My Account' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'Registration' => 'not-logged-users' ) );
}
I know this isn't correct, but I would love some help with it, please let me know if I'm missing any information.
Share Improve this question edited Feb 12, 2019 at 14:37 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Feb 12, 2019 at 14:29 user155484user155484 2 |2 Answers
Reset to default 1I should suggest a custom walker to, not that hard at all.
starting with: https://developer.wordpress/reference/functions/wp_get_nav_menu_items/
in the foreach loop you are able to check based on title.
in this part you will use: https://developer.wordpress/reference/functions/is_user_logged_in/
foreach($nav_items as $item):
if($item->title == 'Register'):
if(is_user_logged_in()):
//Do your thing when logged in
else:
//Do your thing when not logged in
endif;
endif;
endforeach;
Above is an example, there are multiple ways you can do this :) hope it helps you
These are the plugins I use most often to show/hide menu items based on user role or weather the user is logged in or out:
https://wordpress/plugins/user-menus/ - Simple and intuitive.
https://wordpress/plugins/conditional-menus/ - For more advanced conditions and flexibility.
I also just found this one:
https://wordpress/plugins/if-menu/ - I have not tested it but it has better reviews and was updated more recently than my first suggestion.
wp_nav_menu()
function to exclude specific menu items. – WebElaine Commented Feb 12, 2019 at 14:45