Site logo

Archived topic

Conditional menus

11 replies · Started by Stew on June 26, 2018

Viewing posts 1–12 of 12

Hi,

I want to have some conditional menus (menus on specific pages), unfortunately the plugins that do this are all very much out of date. I've found some PHP, I just want to know what I need to change. Here is a reference to what I'm talking about: https://stackoverflow.com/questions/7816282/wordpress-specific-menus-for-specific-page

I'd like to know what I need to put in there for wp_nav to get it to identify the nav menu properly so it can change it.

Here is some code for beaver builder to change menus on specific pages:

// Conditionally change menus
add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args = '' ) {
	// change the menu in the Header menu position
	if( $args['theme_location'] == 'header' && is_page('1159') ) { 
		$args['menu'] = '32'; // 32 is the ID of the menu we want to use here
	}
	return $args;
}

I would like to do it without a plugin, that also doesn't seem to have specific page selections. Mainly seems to have to do with user roles

Hi Stew,

I'm not sure if that code will work - I'm not seeing it on the stackoverflow page you linked to?

If it does, you'll need to update the theme_location value from header to primary.

Then you'll need to make sure the is_page() condition is true (is that the page ID?), and the menu ID is correct.

That code was taken from another site (https://beyondbeaver.com/wordpress-different-main-menu-on-different-pages/) it was just another example.

So the code should be, replacing the placeholder text with the true IDs. I feel like that probably won't work because the bb_nav_menu seems to be specific to beaver builder, which I'm not using.

// Conditionally change menus
add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args = '' ) {
	// change the menu in the Header menu position
	if( $args['theme_location'] == 'primary' && is_page('page id here') ) { 
		$args['menu'] = 'menu id here'; 
	}
	return $args;
}

In theory that should work.

bb_wp_nav_menu_args is just the function name - you can make that whatever you like.

Edit: operator error

I have it working by using the above code

Awesome :)

Hi

Is it possible to customize this function for:
Off canvas panel;
Secondary navigation;

Sure, in that code, find this:

$args['theme_location'] == 'primary'

For the secondary nav, you'd do this:

$args['theme_location'] == 'secondary'

For the off canvas panel, you'd do this:

$args['theme_location'] == 'slideout'

Hi, sorry for re-open. This code works but only for first ID. I need more ID´s of page and i need specific category also. How I updated code?

// Conditionally change menus
add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args = '' ) {
	// change the menu in the Header menu position
	if( $args['theme_location'] == 'primary' && is_page('14901,15068,15058,15060,15062,15066,15064,15070,15072,15056,14916,15074,15076') ) { 
		$args['menu'] = '756'; 
	}
	return $args;
}

Hi Jan,

Can you open up a new topic?

We'll make sure to address you there. Thanks.

This archived topic is closed to new replies.