askdesign
11/14/2015 - 2:35 AM

How to conditionally replace Navigation menu in Genesis

Sridhar Katakam

// In this article I share the code snippet to display a specific custom menu in nav bar on a specific Page (About) while the Primary Navigation in rest of the site’s pages show a different menu as set in Appearance > Menus > Manage Locations.
<?php
//* Do NOT include the opening php tag
add_action( 'genesis_before', 'sk_replace_menu_in_primary' );
/**
 * Conditionally replace Custom Menu in Primary Navigation.
 *
 * @author Sridhar Katakam
 * @link http://sridharkatakam.com/conditionally-replace-navigation-menu-genesis/
 */
function sk_replace_menu_in_primary() {
	if( is_page( 'about' ) ) { // Put your conditional here
		add_filter( 'wp_nav_menu_args', 'replace_menu_in_primary' );
	}
}
function replace_menu_in_primary( $args ) {
	if ( $args['theme_location'] == 'primary' ) {
		$args['menu'] = 'About Page Menu'; // Name of the custom menu that you would like to display in Primary Navigation location when the condition in earlier function is met
	}
	return $args;
}

// If you would like to do similarly with a menu in Secondary Navigation Menu location, replace primary with secondary in the above.