Instantly share code, notes, and snippets.
Source code for https://victorfont.com/genesis-conditional-menus-revisited/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* When a new page is selected from the primary menu, | |
//* execute the change_primary_menu function | |
add_action( 'template_redirect', 'change_primary_menu' ); | |
function change_primary_menu() { | |
//* Do nothing if menu not supported | |
if ( ! genesis_nav_menu_supported( 'primary' ) ) | |
return; | |
add_filter( 'wp_nav_menu_args', 'choose_primary_nav_menu' ); | |
} | |
function choose_primary_nav_menu( $args ) { | |
if ( $args['theme_location'] == 'primary' ) { | |
//* default primary menu | |
$args['menu'] = 'Menu1'; | |
if( is_page( 37 ) ) { | |
$args['menu'] = 'Menu2'; | |
} | |
if( is_page( 39 ) ) { | |
$args['menu'] = 'Menu3'; | |
} | |
} | |
return $args; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* When a new page is selected from the primary menu, | |
//* execute the change_secondary_menu function | |
add_action( 'template_redirect', 'change_secondary_menu' ); | |
function change_secondary_menu() { | |
//* Do nothing if menu not supported | |
if ( ! genesis_nav_menu_supported( 'secondary' ) ) | |
return; | |
add_filter( 'wp_nav_menu_args', 'choose_secondary_nav_menu' ); | |
} | |
function choose_secondary_nav_menu( $args ) { | |
if ( $args['theme_location'] == 'secondary' ) { | |
//* default secondary menu | |
$args['menu'] = 'Menu1'; | |
if( is_page( 37 ) ) { | |
$args['menu'] = 'Menu2'; | |
} | |
if( is_page( 39 ) ) { | |
$args['menu'] = 'Menu3'; | |
} | |
} | |
return $args; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* When a new page is selected from the primary menu, | |
//* execute the change_secondary_menu function | |
add_action( 'template_redirect', 'change_secondary_menu' ); | |
function change_secondary_menu() { | |
//* Do nothing if menu not supported | |
if ( ! genesis_nav_menu_supported( 'secondary' ) ) | |
return; | |
add_filter( 'wp_nav_menu_args', 'choose_secondary_nav_menu' ); | |
} | |
function choose_secondary_nav_menu( $args ) { | |
if ( $args['theme_location'] == 'secondary' ) { | |
//* default secondary menu | |
$args['menu'] = 'Menu1'; | |
if( is_page() && is_subpage() == 37 ) { | |
$args['menu'] = 'Menu2'; | |
} | |
if( is_page() && is_subpage() == 39 ) { | |
$args['menu'] = 'Menu3'; | |
} | |
} | |
return $args; | |
} | |
function is_subpage() { | |
global $post; // load details about this page | |
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent | |
return $post->post_parent; // return the ID of the parent post | |
} else { // there is no parent so ... | |
return 0; // ... the answer to the question is false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment