[Resolved] Conditional menus

Home Forums Support [Resolved] Conditional menus

Home Forums Support Conditional menus

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #609390
    Stew

    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;
    }
    
    #609481
    Leo
    Staff
    Customer Support

    Hi 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 🙂

    #609490
    Stew

    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

    #609493
    Tom
    Lead Developer
    Lead Developer

    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.

    #609498
    Stew

    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;
    }
    #609502
    Tom
    Lead Developer
    Lead Developer

    In theory that should work.

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

    #609507
    Stew

    Edit: operator error

    I have it working by using the above code

    #609977
    Tom
    Lead Developer
    Lead Developer

    Awesome 🙂

    #1044586
    Rolandas

    Hi

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

    #1044689
    Tom
    Lead Developer
    Lead Developer

    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'

    #1496206
    Royal Rangers

    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;
    }
    #1496213
    Elvin
    Staff
    Customer Support

    Hi Jan,

    Can you open up a new topic?

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

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.