- This topic has 11 replies, 2 voices, and was last updated 3 years, 6 months ago by Elvin.
-
AuthorPosts
-
June 26, 2018 at 3:58 pm #609390Stew
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; }
June 26, 2018 at 8:09 pm #609481LeoStaffCustomer SupportHi there,
Have you tried this one here?
https://wordpress.org/plugins/if-menu/It’s the one we usually suggest and it looks like it’s up to date.
Let me know 🙂
June 26, 2018 at 8:20 pm #609490StewI 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
June 26, 2018 at 8:29 pm #609493TomLead DeveloperLead DeveloperHi 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 fromheader
toprimary
.Then you’ll need to make sure the
is_page()
condition is true (is that the page ID?), and the menu ID is correct.June 26, 2018 at 8:44 pm #609498StewThat 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; }
June 26, 2018 at 8:48 pm #609502TomLead DeveloperLead DeveloperIn theory that should work.
bb_wp_nav_menu_args
is just the function name – you can make that whatever you like.June 26, 2018 at 9:12 pm #609507StewEdit: operator error
I have it working by using the above code
June 27, 2018 at 9:28 am #609977TomLead DeveloperLead DeveloperAwesome 🙂
October 25, 2019 at 1:05 pm #1044586RolandasHi
Is it possible to customize this function for:
Off canvas panel;
Secondary navigation;October 25, 2019 at 4:48 pm #1044689TomLead DeveloperLead DeveloperSure, 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'
October 19, 2020 at 1:51 pm #1496206Royal RangersHi, 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; }
October 19, 2020 at 1:56 pm #1496213ElvinStaffCustomer SupportHi Jan,
Can you open up a new topic?
We’ll make sure to address you there. Thanks.
-
AuthorPosts
- You must be logged in to reply to this topic.