[Resolved] Conditional menu

Home Forums Support [Resolved] Conditional menu

Home Forums Support Conditional menu

  • This topic has 3 replies, 2 voices, and was last updated 12 months ago by Elvin.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1496218
    Jan Pecenka

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

    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;
    }

    A wise man once said:
    "Have you cleared your cache?"

    #1496260
    Jan Pecenka

    Yes, it works perfectly. Thanks.

    #1496276
    Elvin
    Staff
    Customer Support

    Yes, it works perfectly. Thanks.

    Nice one. No problem. 🙂

    A wise man once said:
    "Have you cleared your cache?"

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