Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active April 22, 2021 11:48
Show Gist options
  • Save srikat/5d40d43cd2df122fed41 to your computer and use it in GitHub Desktop.
Save srikat/5d40d43cd2df122fed41 to your computer and use it in GitHub Desktop.
Showing a different menu in Primary Navigation location conditionally in Genesis
<?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;
}
@srikat
Copy link
Author

srikat commented May 2, 2017

@asodipo

Place the code in child/active theme's functions.php.

You need to replace

  1. if( is_page( 'about' ) ) {

with the if conditional where you want to display another nav menu in the Primary Navigation location.

For example:

Changing it to

if ( is_home() ) {

will display the menu you're going to set in L21 when you visit your Posts page (the Page set at Settings > Reading).

  1. About Page Menu in

$args['menu'] = 'About Page Menu';

with the name of the custom menu you'd like to be shown in the Primary Navigation location when on the page set in #1 above.

@mparraud
Copy link

Thank you, it worked perfectly. I replaced is_page with !is_front_page because I needed a different menu for all pages but the front page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment