Paolo Bortolotti
Paolo Bortolotti

Reputation: 1

Wordpress Display ACF only in first depth nav

I write function for display acf in menu. I need to display only in depth [0]. All work. But i still to se terrible notice:
Notice: Undefined index: nav_menu_item_depth in

here is my code:

add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types($choices)
{
    $choices['Menu']['menu_level'] = 'Livello Menu';
    return $choices;
}

add_filter('acf/location/rule_values/menu_level', 'acf_location_rule_values_level');

function acf_location_rule_values_level($choices)
{
    $choices[0] = '0';
    $choices[1] = '1';

    return $choices;
}

add_filter('acf/location/rule_match/menu_level', 'acf_location_rule_match_level', 10, 4);
function acf_location_rule_match_level($match, $rule, $options, $field_group)
{
  global $current_screen;
    if ($current_screen->id == 'nav-menus') {
        if ($rule ['operator'] == "==") {
            $match = ($options['nav_menu_item_depth'] == $rule['value']); // <-- Problem is here
        }
    }
    return $match;
}

Some can help me to understand? Thanks

Upvotes: 0

Views: 797

Answers (1)

Tommaso Boggia
Tommaso Boggia

Reputation: 159

Ciao Paolo! It worked for me by adding an isset() check on $options['nav_menu_item_depth']. My guess is that this runs on the whole nav-menus page before hitting the nav-items.

function acf_location_rule_match_level($match, $rule, $options, $field_group)
{
  global $current_screen;
    if ($current_screen->id == 'nav-menus' && isset($options['nav_menu_item_depth'])) {
        if ($rule ['operator'] == "==") {
            $match = ($options['nav_menu_item_depth'] == $rule['value']); // <-- Problem is here
        }
    }
    return $match;
}

I'm pretty new to WP so I'd love to hear if there is a better way to do this or if this is a bad idea for some reason.

Upvotes: 0

Related Questions