Site logo

Archived topic

Conditional menu

3 replies · Started by Royal Rangers on October 19, 2020

Viewing posts 1–4 of 4

I try this code. It 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,

You can try adding making your page/post id list into an array.

Example: from is_page('1,2,3'), change to is_page( array(1,2,3) ).

You can also try adding more condition parameters like is_category().

Example: || is_category('category_slug')'

|| is PHP's "or" logical operator. category_slug can be changed to your target category slug.

Knowing these things, we can change the code into this:

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( array(14901,15068,15058,15060,15062,15066,15064,15070,15072,15056,14916,15074,15076) ) || is_category('category_slug') ) { 
		$args['menu'] = '756'; 
	}
	return $args;
}

Yes, it works perfectly. Thanks.

Yes, it works perfectly. Thanks.

Nice one. No problem. :)

This archived topic is closed to new replies.