0

I have the following code to show different menus to logged in members. I would like to add a second menu to be displayed when logged in, for a total of 3 menus, as there are two different menu areas for logged-in users.

function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {

$args['menu'] = 'menu-1';

} else {

$args['menu'], $args['menu'] = 'menu-3';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

When using this code as-is, "menu-1" will display twice in the 2 areas. I can not figure out the syntax to add another line to include "menu-2". Can someone let me know what to change?

1 Answer 1

1

I suggest you use the theme_location options in WordPress. Assuming you have multiple locations for navigation menus what you could do is assign the two different menus to two different locations. Then adjust your theme templates to display the menu based on logged-in status rather than trying to add two menus to the same location. If you don't have custom theme locations then you can create them with the register_nav_menu function. That would look like this:

    add_action( 'after_setup_theme', 'register_my_menu' );
    function register_my_menu() {
      register_nav_menu( 'logged-in-menu', __( 'Logged In Menu One', 'theme-slug' ) );
    }

Then you will need to edit the theme template which has the nav menus which most likely be located in the following folder: wp-content/themes/theme-name. (You should be using a child theme to make these changes if you aren't already) In that template, there should be a wp_nav_menu() function call that outputs the menu. To show or hide conditionally based on logged-in status it would look something like this:

<div class="header-nav-menu-one> 
<?php if(is_user_logged_in()){
wp_nav_menu(array('theme_location' => 'logged-in-menu',...other args))
} ?> 
</div> 
<div class="regular-menu"> 
<?php wp_nav_menu(array('theme_location' => 'primary',...other args)) ?> 
</div>

Not sure that is the best solution but I think it would accomplish what you are trying to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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