I am building out a mega menu for a site and I have some ACF fields set up that I want to display in each of the sub-menus in the navigation. I want to specify which field to used based on which parent menu item's sub-menu is currently active. I have started building out a custom nav walker but I'm not really sure how I can go about conditionally getting those custom fields based on the parent menu item.

I have my walker set up like this to prepare the sub-menu container elements:

class Sub_Menu extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class=\"sub-menu-container\"><ul class=\"sub-menu\">\n";
    function end_lvl(&$output, $depth = 0, $item, $args = array()) {
        // Conditionaly get field based on parent element
        $sub_menu_fields = get_field('lorem', 'option');
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
        foreach ($sub_menu_fields as $field) :
            $output.= "<div>Custom sub-menu content goes here</div>";
        $output .= "</div>\n";

Now what I am trying to do is to pass a dynamic ACF field name into my get_field() call based on the parent menu item's name. I think I am going to need to use start_el() for this, so I added this:

function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
    $parent_item = $object->post_name;

This gets me the slug of each of the menu items but I'm not sure how to combine this to conditionally select which field to use in the end_lvl() function. Any ideas?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.