Walker class for sub-menu with ACF fields

Firstly, I'm wondering if this is the best way to even do this.

I have a basic nav, one item has children.

The children need to be wrapped in a container div and each child needs to pull some ACF values from its associated page (image, ACF repeater values). The children are basically tiles with content + background images.

Is a walker class the best way to achieve this? The only thing I can manage to do is affect all nav items, I'm unsure of how to make it so that children items are affected and bring in the ACF values.

Thanks

Topic advanced-custom-fields walker children custom-field menus Wordpress

Category Web


Yes, if you want to keep using a WP nav menu, a walker is the right way to go.

To only affect a certain level (i.e. children, grandchildren, parent, etc.) you can use $depth.

class wpseWalker extends Walker_Nav_Menu {
    public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        // Check $depth - if it's 0 it's the top parent, 1 is a direct child
        if($depth == 1) {
            // Just temporarily, show all the item's properties
            // You'll want to grab the ID from here and pull the ACF data
            $output .= print_r($item, true);
        }
    }
}

You'll also want to check and make sure the ACF data exists, in case someone sets up a menu item that doesn't have that info.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.