Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display a different nav menu for logged in or logged out users? WordPress

Using the Genesis theme framework, how do i display a different nav menu for logged in and logged out users in the same position?

if( is_user_logged_in() ) {
wp_nav_menu( array( 'theme_location' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'theme_location' => 'not-logged-users' ) );
}

Answers (6)

2013-03-18

Arnav Joy answers:

check this

http://journalxtra.com/easyguides/add-menu-genesis-themes-4830/


Brad Dalton comments:

Not what i need Arnav.

I need the code to display on of 2 nav menus in the same location depending on the user being logged in or logged out.

The logged in menu includes a link to a directory for members only the logged out version doesn't.


Arnav Joy comments:

try this in child theme functions.php file

<?php
add_action('genesis_after_header', 'change_menu');
function change_menu() {
if( is_user_logged_in() ) {
wp_nav_menu( array( 'theme_location' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'theme_location' => 'not-logged-users' ) );
}
}

remove_action( 'genesis_after_header', 'genesis_do_nav' );
remove_action( 'genesis_after_header', 'genesis_do_subnav' );

?>


Arnav Joy comments:

you can also try adding one menu item to logged in user using following code:-

<?php

add_filter( 'genesis_nav_items', 'add_item', 10, 2 );
add_filter( 'wp_nav_menu_items', 'add_item', 10, 2 );

function add_item($menu, $args) {
if( is_user_logged_in() ) {
$new_item = '<li id="new_item"><a href="http://example.com/mem_dir">Member Directory Page</a></li>';
return $menu . $new_item;
}
else
return $menu;

}

?>


Brad Dalton comments:

Paul's solution works however the code needs to be duplicated and the function names changed for members and non members nav menu's.


Brad Dalton comments:

Arnav Joy. The first block of code doesn't work properly. It displays the nav menu vertically.

The second code snippet removes the nav menu for both logged in and logged out users.

Please correct me if i am wrong but i have tested all these code snippets.

2013-03-18

paul de wouters answers:

first create you default menu and assign it to the primary location, then create a members only menu and don't assign it to a location
then, can you try this code
http://pastebin.com/1hfpgmfw


paul de wouters comments:

Brad

why would you need to duplicate it? Maybe I didn't understand your requirement.
Do you want it to work with multiple menu locations?


Brad Dalton comments:

You don't for one menu location correct.

2013-03-18

Daniel Yoen answers:

add_action('template_redirect', 'child_conditional_actions');
function child_conditional_actions() {
if (is_user_logged_in() )
remove_action('genesis_after_header', 'genesis_do_subnav');
}


hope this help

http://codex.wordpress.org/Conditional_Tags


Daniel Yoen comments:

complete hook

http://wpsites.net/wordpress-admin/nav-menu-genesis-footer/#Display_Second_Nav_Menu_After_Footer

:-)

2013-03-18

Dbranes answers:

If you want to add an item to your <strong>wp_nav_menu()</strong> you could try something like:

add_filter( 'wp_nav_menu_items', 'custom_nav_link', 10, 2 );
function custom_nav_link($items, $args) {
if ( is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li class="members-only"><a href="http://example.com/members_only" title="member only">Members only</a></li>';
}
return $items;
}


Brad Dalton comments:

This is another good solution which works.

2013-03-18

Abdelhadi Touil answers:

Hi.
In your situation, the best solution I use is the If Menu, it's very useful in such situation:

[[LINK href="http://wordpress.org/extend/plugins/if-menu/"]]http://wordpress.org/extend/plugins/if-menu/[[/LINK]]

Good luck :)

2013-03-21

surya answers:

Menu by User Role for WordPress plugin works for u

Public - not logged in users
Loggedin - logged in users