askdesign
11/14/2015 - 2:17 AM

Assign menus conditionally in Secondary Navigation Menu location

Sridhar Katakam - Zamir option #1 Implemented

// https://sridharkatakam.com/conditionally-assigning-different-menus-in-secondary-navigation-menu-location-in-genesis/
// STEP 1 of original code 
// Returns true when we are on a Page in question or any of its sub Pages
// https://codex.wordpress.org/Function_Reference/is_page#Testing_for_sub-Pages
function is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
	global $post;               // load details about this page

	if ( is_page( $pid ) )
		return true;            // we're at the page or at a sub page

	$anc = get_post_ancestors( $post->ID );
	foreach ( $anc as $ancestor ) {
		if( is_page() && $ancestor == $pid ) {
			return true;
		}
	}

	return false;  // we aren't at the page, and the page is not an ancestor
}

// Now is_tree( ‘7’ ), for example, returns true if the current page is ‘Shop’ (this has the ID of 7) or any of its sub Pages: ‘Product 1’ and ‘Product 2’.
// Next go ahead and create several menus for the different views.
// At Appearance > Menus > Manage Locations, assign your main or default menu to Secondary Navigation Menu location.

// STEP 2 of original code
// Assign menus conditionally in Secondary Navigation Menu location
add_filter( 'wp_nav_menu_args', 'replace_menu_in_secondary' );
function replace_menu_in_secondary( $args ) {
	if ( $args['theme_location'] != 'secondary' ) {
		return $args;
	}

	if( is_tree( '7' ) ) { // Shop Page or any of its sub pages
		$args['menu'] = 'Shop Menu';
	} elseif ( is_tree( '10' ) ) { // Video Page or any of its sub pages
		$args['menu'] = 'Video Menu';
	} elseif ( tribe_is_event_query() ) { // any events related page
		$args['menu'] = 'Events Menu';
	} elseif ( is_tree( '8' ) ) { // About Page or any of its sub pages
		$args['menu'] = 'About Menu';
	}

	return $args;
}

// IMPORTANT NOTE: after adding the code below to functions.php, BE SURE TO CREATE AN EMPTY SECONDARY NAV MENU IN WORDPRESS
// Name should be: "Secondary Menu (Secondary Navigation Menu)"
// Assign it to the secondary nav menu theme location

// https://sridharkatakam.com/conditionally-assigning-different-menus-in-secondary-navigation-menu-location-in-genesis/
// Adapted to use with wp emember code and Zamir site
// Assign menus conditionally in Secondary Navigation Menu location
add_filter( 'wp_nav_menu_args', 'replace_menu_in_secondary' );
function replace_menu_in_secondary( $args ) {
	if ( $args['theme_location'] != 'secondary' ) {
		return $args;
	}

if ( wp_emember_is_member_logged_in('2') ) {//Show this menu to members of membership level 2
		$args['menu'] = 'Tutti';
	} else if ( wp_emember_is_member_logged_in('3') ) {//Show this menu to members of membership level 3
		$args['menu'] = 'Board';
	} else if ( wp_emember_is_member_logged_in('4') ) {//Show this menu to members of membership level 4
		$args['menu'] = 'Staff';
	}
	return $args;
}