最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hide menu item when users are logged inout

programmeradmin2浏览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.

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
  • 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/questions/233667/… – rudtek Commented Feb 12, 2019 at 15:28
Add a comment  | 

2 Answers 2

Reset to default 1

I 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.

发布评论

评论列表(0)

  1. 暂无评论