[Resolved] Seperate Primary and Secondary Menu for Logged In vs Logged Out Users

Home Forums Support Seperate Primary and Secondary Menu for Logged In vs Logged Out Users

  • This topic has 11 replies, 2 voices, and was last updated 2 months ago by Tom.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1112438
    Robert Cioffi

    I’m using a function in a site specific plugin to do this with the primary menu. Here’s the code:

    function my_wp_nav_menu_args( $args = ” ) {

    if( is_user_logged_in() ) {
    $args[‘menu’] = ‘Main’;
    } else {
    $args[‘menu’] = ‘Main2’;
    }
    return $args;
    }
    add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );`

    But it messes up the secondary menu so I tried this:

    function my_wp_nav_menu_args( $args = ” ) {

    if( is_user_logged_in() ) {
    $args[‘primary’] = ‘Main’;
    $args[‘secondary’] = ‘Header’;
    } else {
    $args[‘primary’] = ‘Main2’;
    $args[‘secondary’] = ‘Header2’;
    }
    return $args;
    }
    add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );

    But that only worked for the logged in users (I think).

    Any way I can make this work?

    Thanks.

    #1112809
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    What if we target the primary navigation?:

    add_filter( 'wp_nav_menu_args', function( $args ) {
        if ( 'primary' === $args->theme_location ) {
            if ( is_user_logged_in() ) {
                $args['menu'] = 'Main';
            } else {
                $args['menu'] = 'Main2';
            }
        }
    
        return $args;
    } );

    Let me know 🙂

    #1113964
    Robert Cioffi

    Did not work. Let me take a step back and explain what I’m trying to do. I have a primary nav and right below it a secondary nav. I’d like the logged in users to see one set of menus and the logged out users to see a different set. I named the menus Menu and Header for the logged in users and Menu2 and Header2 for the non logged in users.

    I googled and found then followed this article by creating a function inside of a site specific plugin.

    https://www.wpbeginner.com/wp-themes/how-to-show-different-menus-to-logged-in-users-in-wordpress/

    Here’s the plugin code I used:

    <?php
    /*
    Plugin Name: Site Plugin for example.com
    Description: Site specific code changes for example.com
    */
    /* Start Adding Functions Below this Line */

    function my_wp_nav_menu_args( $args = ” ) {

    if( is_user_logged_in() ) {
    $args[‘menu’] = ‘Main’;
    } else {
    $args[‘menu’] = ‘Main2’;
    }
    return $args;
    }
    add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );

    /* Stop Adding Functions Below this Line */
    ?>

    It worked for the primary nav (Logged In> Main and Logged Out > Main2) but unfortunately it populates the secondary nave with Main & Main2. I need the secondary nav to be Logged In > Header and Logged Out > Header2.

    There must be a way for this to work for both primary nav and secondary nav, yes?

    Thanks for your help.

    #1114090
    Tom
    Lead Developer
    Lead Developer

    Does my code work with the primary navigation? You can target the secondary nav as well:

    add_filter( 'wp_nav_menu_args', function( $args ) {
        if ( 'primary' === $args['theme_location'] ) {
            if ( is_user_logged_in() ) {
                $args['menu'] = 'Main';
            } else {
                $args['menu'] = 'Main2';
            }
        }
    
        if ( 'secondary' === $args['theme_location'] ) {
            if ( is_user_logged_in() ) {
                $args['menu'] = 'Header';
            } else {
                $args['menu'] = 'Header2';
            }
        }
    
        return $args;
    } );
    #1114238
    Robert Cioffi

    I tried your code in the site specific plugin and the result was primary nav was Main and secondary nav was Header whether the user was logged in or logged out. Very puzzling.

    Thanks.

    #1114556
    Tom
    Lead Developer
    Lead Developer

    Strange – can you try the edited code above?

    #1114587
    Robert Cioffi

    That seems to be working. Thanks Tom!

    #1114594
    Tom
    Lead Developer
    Lead Developer
    #1370077
    Arp

    @Tom is it possible to target the off-canvas menu location as well?

    #1370172
    Tom
    Lead Developer
    Lead Developer

    Absolutely, just use slideout instead of primary.

    #1371126
    Arp

    Sweet, I wasn’t sure what to call it – thanks!

    #1371426
    Tom
    Lead Developer
    Lead Developer
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.