Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1398198

    Hi everyone – I am working on a site rebuild which has 3 areas for different audiences. This requires the main nav to change based on what area you are in. I have searched around on the forum and saw some old references to different plugins that might do this, but all those plugins seem to no longer be developed (2015/2018 were the last versions). I therefore think I need to modify the helper-main-menu script via a child theme and write some custom code for this. Is there a better way of doing this via a via function, or is there a plugin that would work? What I am trying to achieve is to create 3 menus, and based on which page you are viewing, if it is a sub-page of one of 3 pages then show the relevant menu. Any help would be really appreciated. Thanks.

    #1398322

    ***SOLVED****
    Further to this, for anyone wanting to solve this challenge of having conditional man navigation menus based on the site section, the below set of functions:

    1. Setup specific menus for your audiences (Patient and HCP in this example)
    2. Creates an is_child function that determines if the page has a parent page called something specific (you can also do this by page ID or page slug of the parent but I want to re-use it for other sites so using the page name)
    3. Provides the conditional swap logic “my_custom_wp_nav_menu_args” to swap out the main nav and defaults to the Enfold Main Nav otherwise.

    So to set this up:

    Create the following in the child theme functions.php
    Create your section home pages
    Create your sub-pages and then set their parent page as the section home page

    I hope this helps someone.

    function register_my_menus() {
    register_nav_menus(
    array(
    ‘patient-menu’ => __( ‘Patient Menu’ ),
    ‘HCP-menu’ => __( ‘HCP Menu’ )
    )
    );
    }
    add_action( ‘init’, ‘register_my_menus’ );

    /**
    * Child page conditional
    * @ Accept’s page ID, page slug or page title as parameters
    * https://sridharkatakam.com/useful-functions-checking-pages-sub-pages-wordpress/
    */
    function is_child( $parent = ” ) {
    global $post;
    $parent_obj = get_page( $post->post_parent, ARRAY_A );
    $parent = (string) $parent;
    $parent_array = (array) $parent;
    if ( in_array( (string) $parent_obj[‘ID’], $parent_array ) ) {
    return true;
    } elseif ( in_array( (string) $parent_obj[‘post_title’], $parent_array ) ) {
    return true;
    } elseif ( in_array( (string) $parent_obj[‘post_name’], $parent_array ) ) {
    return true;
    } else {
    return false;
    }
    }

    add_filter( ‘wp_nav_menu_args’, ‘my_custom_wp_nav_menu_args’ );

    /**
    * CManu switch based on page/parent relationship for HCP, Patient or others
    * @ Accept’s page ID, page slug or page title as parameters
    * PJE – RESOLVE
    */

    function my_custom_wp_nav_menu_args( $args = ” ) {

    if($args[‘theme_location’] === ‘avia’)
    if ( is_child( ‘HCP Home’ ) ) {
    $args[‘menu’] = ‘HCP’;
    } elseif ( is_child( ‘Patient Home’ ) ) {
    $args[‘menu’] = ‘Patient’;
    } else {
    //* otherwise set the default as defined in the enfold-child theme as Main Menu
    $args[‘menu’] = ‘avia-menu’;
    }
    return $args;
    }

    #1398330

    Hi,

    Thanks for sharing your solution!

    For future readers, we also have a code snippet here: https://kriesi.at/documentation/enfold/menu/#different-menu-for-different-pages

    Let us know if you have any other questions and enjoy the rest of your day!

    Best regards,
    Yigit

    #1398335

    Thanks Yigit – unless I have misunderstood – your solution just looks at having different menus (Main nav, top nav, footer etc). This creates a different MAIN Nav for a section based on the conditions of where the page is in the hierarchy.

    I am currently trying to hook into the very top nav to do the same. (header_secondary_menu) which I see you have avf_execute_avia_meta_header

    You define this as avia2 in the helper-main-menu.php (lines 62-64)

    //display the small submenu
          $avia_theme_location = 'avia2';
    			                $avia_menu_class = $avia_theme_location . '-menu';

    So I have modified the following but it doesn’t appear to work. What am I missing?

    Thanks

    function my_custom_wp_nav_menu_args( $args = '' ) {
    
    if($args['theme_location'] === 'avia2') 
    	if ( is_child( 'HCP Home' ) ) {
    		  $args['menu'] = 'Top Menu HCP'; 
    	} elseif ( is_child( 'Patient Home' ) ) {
    		  $args['menu'] = 'Top Menu Patient'; 		
    	} else {
    		//* otherwise set the default as defined in the enfold-child theme as Secondary Menu
    		$args['menu'] = 'avia2';
    	}
    return $args;
    #1398484

    Hi,
    Thanks for your patience, I tested your function on my demo site for the top menu avia2 for my demo site the patient page portfolio and all of it child pages show [‘menu’] = ’11’; and for the parent page elements and all of it child pages show [‘menu’] = ’36’;, all other page show [‘menu’] = ’37’;.
    This is the code that worked for me:

    function my_custom_wp_nav_menu_args( $args = '' ) {
    
    if($args['theme_location'] === 'avia2')
    if ( is_child( 'portfolio' ) ) {
    $args['menu'] = '11';
    } elseif ( is_child( 'elements' ) ) {
    $args['menu'] = '36';
    } else {
    //* otherwise set the default as defined in the enfold-child theme as Main Menu
    $args['menu'] = '37';
    }
    return $args;
    }

    try using the menu IDs instead of the names, that is the only difference I see from your code.
    This is the full code I used:

    function register_my_menus() {
    register_nav_menus(
    array(
    'patient-menu' => __( 'Patient Menu' ),
    'HCP-menu' => __( 'HCP Menu' )
    )
    );
    }
    add_action( 'init', 'register_my_menus' );
    
    /**
    * Child page conditional
    * @ Accept's page ID, page slug or page title as parameters
    * https://sridharkatakam.com/useful-functions-checking-pages-sub-pages-wordpress/
    */
    function is_child( $parent = '' ) {
    global $post;
    $parent_obj = get_page( $post->post_parent, ARRAY_A );
    $parent = (string) $parent;
    $parent_array = (array) $parent;
    if ( in_array( (string) $parent_obj['ID'], $parent_array ) ) {
    return true;
    } elseif ( in_array( (string) $parent_obj['post_title'], $parent_array ) ) {
    return true;
    } elseif ( in_array( (string) $parent_obj['post_name'], $parent_array ) ) {
    return true;
    } else {
    return false;
    }
    }
    
    add_filter( 'wp_nav_menu_args', 'my_custom_wp_nav_menu_args' );
    
    /**
    * CManu switch based on page/parent relationship for HCP, Patient or others
    * @ Accept's page ID, page slug or page title as parameters
    * PJE – RESOLVE
    */
    
    function my_custom_wp_nav_menu_args( $args = '' ) {
    
    if($args['theme_location'] === 'avia2')
    if ( is_child( 'portfolio' ) ) {
    $args['menu'] = '11';
    } elseif ( is_child( 'elements' ) ) {
    $args['menu'] = '36';
    } else {
    //* otherwise set the default as defined in the enfold-child theme as Main Menu
    $args['menu'] = '37';
    }
    return $args;
    }

    Best regards,
    Mike

    #1398837

    Mike – thanks so much for that, it worked perfectly.

    Much appreciated.

    Philip.

    #1398858

    Hi Philip,

    Glad Mike could help.

    Let us know if you have any other questions and enjoy the rest of your day.

    Best regards,
    Yigit

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Different main nav menus for site section’ is closed to new replies.