Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Created April 29, 2020 20:27
Show Gist options
  • Save vfontjr/7c37672d77b0f1a487ea39ca34568da6 to your computer and use it in GitHub Desktop.
Save vfontjr/7c37672d77b0f1a487ea39ca34568da6 to your computer and use it in GitHub Desktop.
<?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;
}
<?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;
}
<?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