简体   繁体   中英

How can I edit this code so that it only changes the Top Menu bar on my homepage when a user is logged in without changing the Main Menu?

I am using OceanWP theme to create my site.

I have two different menus on my homepage, I have a top menu and I also have a main menu. http://prntscr.com/pofn5r

I would like for the top menu to display different options for users who are logged in and users who are logged out.

I have used the following code which I placed in the functions.php file. I have also created two different menus for logged in and logged out users:

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' );

It seems to work in terms of showing different menus for users which are logged in and users which are logged out but the problem is that it also changes the main menu as well as the top menu.

prntscr.com/podv5e

I wanted the main menu to remain the same and just have the top menu change.

I was wandering whether there was a way I could adjust the code so that it would only apply to the top menu and not the main menu?

You could do something like:

function my_wp_nav_menu_args( $args = '' ) {

if( is_user_logged_in() ) { 
    echo "Here you put HTML code when logged in";
} else { 
    echo "Here you put HTML code when logged out"
} 
}

since echo will work like it is writted directly into HTML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM