WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Alternative Menu for logged in users (12 posts)

  1. Jason Knight
    Member
    Posted 3 years ago #

    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.

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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....

  3. Jason Knight
    Member
    Posted 3 years ago #

    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...

  4. Jason Knight
    Member
    Posted 3 years ago #

    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:

    http://wordpress.pastebin.com/iyugJLC4

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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

  6. Jason Knight
    Member
    Posted 3 years ago #

    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?

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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

    http://codex.wordpress.org/Function_Reference/wp_nav_menu
    for info on parameters for wp_nav_menu to call to the proper menu in your conditional

  8. Jason Knight
    Member
    Posted 3 years ago #

    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~

  9. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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>
  10. Jason Knight
    Member
    Posted 3 years ago #

    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~

  11. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    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!!!

  12. dsg257
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic