How to Use the Function is_user_logged_in To Display Different Menus?

So, I want to create different menus for people who are logged in or not.

I got a reply that i should use a function

if (is_user_logged_in()) {
      wp_name_menu(array('theme_location' => 'logged_user' ));
} else {
      wp_nav_menu(array('theme_location' => 'new_user' ));
}

So, if I get this right, I put this into functions.php which is located in my child theme (anywhere inside the file?), then I create another menu and where it is “logged_user” and “new_user” I enter the names of the menus?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

i just did this for a site i am working on. first i registered 2 menus in functions.php:

add_action('init','kia_menus');

function kia_menus(){

    register_nav_menus( array('primary-menu' => __( 'Primary Menu for Logged In Users', 'kia_theme' ), 
            'primary-loggedout' => __( 'Primary Menu for Logged Out Visitors', 'kia_theme')
                                    ));
}

and then where i want the 1 menu to appear based on the user’s status (probably in header.php but depends on your theme):

if( !is_user_logged_in() ){
    wp_nav_menu( array( 'theme_location' => 'primary-loggedout' ) );
} else {
    wp_nav_menu( array( 'theme_location' => 'primary-menu' ) );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x