Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
Show one menu for not-logged in users and a different menu for logged in users.

Solution:

1
function display_correct_menu($args = '') {
       if ( !is_user_logged_in() ) { $args['menu'] = wp_get_nav_menu_object( 'menu-not-logged-in' ); }
     else { $args['menu'] = wp_get_nav_menu_object( 'menu-logged-in' ); }
         return $args;
}
add_filter( 'wp_nav_menu_args', 'display_correct_menu' );

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Otto 3 years, 6 months ago.

Assigned support staff: Otto.

Author Posts
January 22, 2017 at 11:50 pm #1187815

edwardS-6

I am trying to: Show one menu for not-logged in users and a different menu for logged in users.

When using code like this in my functions.php, the WPML language switcher disappears from the menu:
function display_correct_menu($args = '') {
if ( !is_user_logged_in() ) {$args['menu'] = 'top-not-logged-in';} else {$args['menu'] = 'top';}
return $args;
}
add_filter( 'wp_nav_menu_args', 'display_correct_menu' );

I assume the problem is because the same filter is being used by WPML in the file: class-wpml-nav-menu.php.

Is there a way to accomplish what I need without changing WPML code?

URL of (my) website where problem appears: hidden link (but you really don't need to visit it)

Thank you very much!

January 23, 2017 at 12:24 pm #1188266

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello,

Thank you for contacting the WPML support!

Did you add a language switcher to each menu (top-not-logged-in and top)? You can add them here WPML > Languages > Language Switcher Options > Menu language switcher

Let me know if that helped, please.

Kind Regards,

Otto

January 23, 2017 at 2:28 pm #1188416

edwardS-6

Both menus show the language switcher when I comment out this code in my functions.php:
add_filter( 'wp_nav_menu_args', 'display_correct_menu' );

The problem appears to be that this filter is also being used by WPML.

I need to show different menus for logged in and not logged in users.

Thanks,

January 23, 2017 at 5:23 pm #1188626

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello,

Thanks for the explanation.

The problem seems to be that 'menu' is a term object and the filter is replacing it with a 'string'.

Please try this instead:

$args['menu'] = wp_get_nav_menu_object( 'top-not-logged-in' );

And the same with 'top'

I tried it locally and it worked fine.

Let me know your results, please.

Kind Regards,

Otto

January 23, 2017 at 9:55 pm #1188798

edwardS-6

This is our solution, thank you so much for the help!

function display_correct_menu($args = '') {
       if ( !is_user_logged_in() ) { $args['menu'] = wp_get_nav_menu_object( 'top-not-logged-in' ); }
     else { $args['menu'] = wp_get_nav_menu_object( 'top' ); }
         return $args;
}
add_filter( 'wp_nav_menu_args', 'display_correct_menu' );

Solved! GREAT! Comments?

January 24, 2017 at 1:54 pm #1189395

Otto
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Nice! I'm glad it helped 🙂

Kind Regards,

Otto