Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active June 30, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikat/ef0ab52195b3a167ac4d to your computer and use it in GitHub Desktop.
Save srikat/ef0ab52195b3a167ac4d to your computer and use it in GitHub Desktop.
Conditionally assigning different menus in Secondary Navigation Menu location in Genesis. https://sridharkatakam.com/conditionally-assigning-different-menus-in-secondary-navigation-menu-location-in-genesis/
// 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
}
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment