• Here is an example. I had a situation where I needed to use different menus based on the page (not my idea!). So I added a Menu ACF field to the page post type. I used the Menu ID as what would be returned.

    In my functions file I registered my menus

    <?php
    register_nav_menus( array(
    ‘primary’ => __( ‘Main Menu Live’, ‘main-menu’ ),
    ‘primaryvirtual’ => __( ‘Main Menu Virtual’, ‘main-menu-virtual’ ),
    ‘naemea’ => __( ‘North America and EMEA Menu’, ‘naemea-menu’ ),
    ‘apac’ => __( ‘APAC Menu’, ‘apac-menu’ )
    ) );

    ?>

    In the header.php file of my theme I then dynamically call the menu.

    <?php

    if (class_exists(‘ACF’)) {
    $main_menu= get_field(‘main_menu’, get_the_ID());
    }

    $theme_location = ‘primary’;

    switch ($main_menu) {
    case 3:
    $theme_location = ‘primary’;

    break;
    case 7:
    $theme_location = ‘naemea-menu’;

    break;
    case 8:
    $theme_location = ‘apac-menu’;

    break;
    default:
    $theme_location = ‘primary’;
    }

    ?>

    Then change the wp_nav_menu this way.

    <?php

    wp_nav_menu(array(
    ‘menu’ => $main_menu,
    ‘theme_location’ => $theme_location,
    ‘depth’ => 3,
    ‘container’ => ‘div’,
    ‘container_class’ => ‘collapse navbar-collapse’,
    ‘container_id’ => ‘bs-navbar-collapse-1’,
    ‘menu_class’ => ‘nav navbar-nav menu-main-menu’,
    ‘fallback_cb’ => ‘WP_Bootstrap_Navwalker::fallback’,
    ‘walker’ => new WP_Bootstrap_Navwalker(),
    ));

    ?>

    Hope that helps those lost in the WordPress php world.

  • The topic ‘An Example of Menu ACF usage’ is closed to new replies.