How to hide an item from a menu to logged out users (without a plugin)

I want to hide an item from a menu if a user is logged out.

I am currently using the below code that achieves this using two separate menus, but to save duplication, I would like to only have to manage one nav menu.

function my_wp_nav_menu_args( $args="" ) {

    if ( is_user_logged_in() ) { 
        $args['menu'] = 'logged-in';
    } else { 
        $args['menu'] = 'logged-out';
    }

    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

Is it possible to hide just one item for a logged out user, rather than doing it the way I currently am?

5 Answers
5

Find the class or id of the menu item that you want to hide.
suppose the class of that menu is logged-in-menu

Then in header.php file of your theme before closing head tag use the below code

<style>
<?php if(! is_user_logged_in() ) : ?>
    .logged-in-menu{
        display: none;
    }
<?php endif; ?>
</style>

Leave a Comment