1

I have one menu that I created called "Main Menu" and only want to show it to users that are logged in. I have only been able to find plugins that allow me to show/hide individual menu items, but not the entire menu. How can I do this?

1

Assuming you have the ability to either edit the theme or child theme, in the file where the menu is called (probably header.php), use something like this:

<?php
    if( is_user_logged_in() ) :
        wp_nav_menu( array(
            'theme_location' => 'header-nav',
            'menu_id'        => 'header-menu',
        ) );
    endif;
?>

That'll be the preferred option because it won't load the navigation at all, so even someone savvy enough to look at the source code/developer console won't see it.

If you don't have the ability to edit the theme/child theme files, then you can do it using CSS:

#site-navigation {
    display:none;
}
body.logged-in #site-navigation{
    display:block;
}

A few notes, the CSS above addresses the <nav> tag for a site I've recently started building, you'll have to look at your source code/developer console and identify which element/container to add display:none; to. The important part is that WordPress adds the .logged-in class to the body tag, specifically to allow us to write different CSS/js rules/functions depending on users being logged in.

Like I said though, the CSS isn't the preferred option if what you're after is preventing users that aren't logged in from being able to locate items with a menu - for that, you'd want to use the conditional check at the top.

Addendum I should also add the you can add an else into the conditional statement and load a different menu, or a login link...

<?php
    if( is_user_logged_in() ) :
        wp_nav_menu( array(
            'theme_location' => 'header-nav',
            'menu_id'        => 'header-menu',
        ) );
    else :
        wp_nav_menu( array(
            'theme_location' => 'header-nav-notloggedin',
            'menu_id'        => 'header-menu-notloggedin',
        ) );
    endif;
?>
3
  • Hi Tony, thanks for the thorough response. Do I need to change "header-nav" or "header-menu" to something else or will those work as is? The theme menu location I'm using is Primary Menu and my menu is called Main Menu. Thanks again! – Peter Apr 12 '20 at 22:57
  • You'll need to change them to match the theme. My guess would be: 'theme_location' => 'primary-menu'. You should check in the theme and locate where the menus are registered to ensure that matches. The menu_id can actually be set in the example above, that's where you establish what the ID of the <ul> element is, so you don't need to match it. In your theme there's be a place that has a 'register_nav_menus()' function, locate that and see what they set as location for the 'Primary Menu'. – Tony Djukic Apr 12 '20 at 23:00
  • Actually, if you look in your header.php file, you'll see what's already there - just take what's there, replace the wp_nav_menu() in the code I gave you and then paste my code in it's place. – Tony Djukic Apr 12 '20 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.