【问题标题】:Wordpress Display ACF only in first depth navWordpress 仅在第一深度导航中显示 ACF
【发布时间】:2022-07-24 14:42:41
【问题描述】:

我编写了在菜单中显示 acf 的函数。 我只需要在深度 [0] 中显示。 所有工作。但我仍然要注意:
注意:未定义索引:nav_menu_item_depth in

这是我的代码:

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;
}

有些可以帮助我理解? 谢谢

【问题讨论】:

    标签: wordpress advanced-custom-fields acfpro wp-nav-menu-item


    【解决方案1】:

    乔保罗!它通过在$options['nav_menu_item_depth'] 上添加isset() 检查对我有用。我的猜测是,在点击导航项之前,它会在整个导航菜单页面上运行。

    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;
    }
    

    我对 WP 很陌生,所以我很想知道是否有更好的方法来做到这一点,或者出于某种原因这是一个坏主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2023-03-08
      • 2019-10-16
      相关资源
      最近更新 更多