• Resolved Jason Knight

    (@hihipattyrogan)


    My Site is located at ClubPinata and since it’s a membership driven site, I want to hide certain menu items depending on whether the user is logged in or not. If a user is logged off all I want him to see is Home, About Us, and Register. If they’re logged in, well they can see all the rest. I’ve tried editing my header theme and look for wp_list_pages but it’s nowhere to be found! Instead what controls this menu is some odd code like this:

    <div id="menu-wrap">
          <?php wp_nav_menu(array('theme_location' => 'main', 'menu_id' => 'menu')); ?>
          </div>

    I managed to make it work at some point, but now I forgot how and can’t get it back.

    The theme I’m using is called Pendulum.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Off the top of my head, a conditional may work

    <div id="menu-wrap">
    <?php
    if ( is_user_logged_in() ) {
        wp_nav_menu(array('theme_location' => 'main', 'menu_id' => 'menu'));
    } else {
        wp_nav_menu(array('theme_location' => 'main', 'menu_id' => 'visitor'));
    };
    ?>
    </div>

    Your theme is using wp’s built in menu creator…. you can make your own menus in appearance->menu

    look for your register nav menu in functions.php and change it to support 2 menus

    // THIS THEME USES wp_nav_menu() IN ONE LOCATION FOR CUSTOM MENU.
    if ( function_exists( 'register_nav_menus' ) ) {
    	$args = array(
    	  'menu' => __( 'Logged In Menu' ),
    	  'visitor' => __( 'Visitor Menu', )
    	);
    	register_nav_menus( $args );
    }

    Then the conditional says to show menu->menu to logged in users, and menu->visitor to everyone else. Then set up each menu in the back end….

    Thread Starter Jason Knight

    (@hihipattyrogan)

    The Famous RVoodoo, sweet,

    I added that line of code you gave me and it returned this error:

    Parse error: syntax error, unexpected ‘)’ in /home/roganbon/public_html/wp-content/themes/pendulum/functions.php on line 34

    P.S. It just bricked my site…

    Thread Starter Jason Knight

    (@hihipattyrogan)

    I restored functions back to how it was and it’s unbricked, and the conditional statement I’m guessing won’t work without that code, but all my functions.php contains is:

    https://wordpress.pastebin.com/iyugJLC4

    Ah, I see the issue. You are going to have to do some sluething…. You need to find where your nav menu is registered, but your functions.php seems to offload everything….

    you’ll need to look in your themes lib/functions folder at various templates to find where wp_nav_menu is set up?

    You’ll need to look in each of the files included off of that functions.php file you posted

    Thread Starter Jason Knight

    (@hihipattyrogan)

    Bingo, I found another functions.php file in there: ?? it has

    function r_register_menus() {
    	register_nav_menus(
    		array(
    			'main' => __('Main Menu')
    		)
    	);
    }
    
    add_action('init', 'r_register_menus');

    Where would I add the code here?

    function r_register_menus() {
    	register_nav_menus(
    		array(
    			'main' => __('Main Menu'),
                                'visitor' => __( 'Visitor Menu' )
    		)
    	);
    }

    should just be able to tack on another menu in the array, and then conditional between main and visitor

    https://codex.www.ads-software.com/Function_Reference/wp_nav_menu
    for info on parameters for wp_nav_menu to call to the proper menu in your conditional

    Thread Starter Jason Knight

    (@hihipattyrogan)

    Well now I have two menus available, and though they are completely different they have the same links. When I’m logged in the menu for a logged in user appears fine. When I log off, that same menu appears, just without styling, check the site to see what I mean… Almost there~

    ah…I had my conditional confused, we need to conditional based on the location, not the id I believe

    <div id="menu-wrap">
    <?php
    if ( is_user_logged_in() ) {
        wp_nav_menu(array('theme_location' => 'main', 'menu_id' => 'menu'));
    } else {
        wp_nav_menu(array('theme_location' => 'visitor', 'menu_id' => 'menu'));
    };
    ?>
    </div>

    Thread Starter Jason Knight

    (@hihipattyrogan)

    Okay, now it’s not broken ?? But… Logged off users still see the same thing logged in ones do,

    EDIT: Nevermind, I forgot to make a new menu for them.

    THANKS! Can I +Rep you or something? They should add that~

    well that’s plain odd…..

    <?php
    if ( is_user_logged_in() ) {
        echo 'Welcome, registered user!';
    } else {
        echo 'Welcome, visitor!';
    };
    ?>

    is a real simple conditional from the codex, maybe test that to ensure we are getting desired results?

    EDIT, just saw your edit, Glad it works. I’m just happy to help someone, I learn more every time I do it!!!

    dsg257

    (@dsg257)

    Hi just found the post after a lot of searching and reading this is what i’ve been looking for
    not brilliant with code but learning all the time

    i wish to have a menu for visitors and a different menu for registered users and this seems the best solution i have found so far
    just a couple of questions

    1. where do i put the <div id=”menu-wrap”> code
    2. is this code compatable with the weaver 2 theme
    3. the function code just replaces the existing code ??

    Thanks for the help you have given on this subject greatly appreciated

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Alternative Menu for logged in users’ is closed to new replies.