0

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.

enter image description here

2
  • You may need to create a custom walker function. There doesn't seem to be anything you can pass into the wp_nav_menu() function to exclude specific menu items.
    – WebElaine
    Commented Feb 12, 2019 at 14:45
  • 1
    Hey Saint Louis, This question has been asked many times. Wpbeginner has a few plugin ideas and on this stack there are several question with answers. I would go with the idea of replacing the whole menu (not just items). wordpress.stackexchange.com/questions/233667/…
    – rudtek
    Commented Feb 12, 2019 at 15:28

2 Answers 2

1

I should suggest a custom walker to, not that hard at all.

starting with: https://developer.wordpress.org/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.org/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

0

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.org/plugins/user-menus/ - Simple and intuitive.

https://wordpress.org/plugins/conditional-menus/ - For more advanced conditions and flexibility.

I also just found this one:

https://wordpress.org/plugins/if-menu/ - I have not tested it but it has better reviews and was updated more recently than my first suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.