【问题标题】:Get custom field ACF in custom menu在自定义菜单中获取自定义字段 ACF
【发布时间】:2019-04-17 05:46:45
【问题描述】:

我在 Wordpress 中有一个菜单,其中包含一个名为“svg”的 ACF 字段

要创建我的菜单,我这样做:

function b_get_menu_id( $location )
{
  $a = get_nav_menu_locations();
  if (isset($a[$location])) return $a[$location];
  return false;
}

function b_get_nav_items($location) {
  $id = b_get_menu_id($location);
  $nav = [];
  $children = [];
  if(!$id) {
    return $nav;
  }
  foreach(wp_get_nav_menu_items($id) as $object) {
    $item = new stdClass();
    $item->url = $object->url;
    $item->label = $object->title;
    $item->id = $object->object_id;
    $item->icon = $object->classes[0];
    $item->parent = intval($object->menu_item_parent);
    $item->children = [];
    if($item->parent){
      $children[] = $item;
    } else {
      $nav[$object->ID] = $item;
    }
  }
  foreach($children as $item) {
    $nav[$item->parent]->children[] = $item;
  }
  return $nav;
}

我这样显示我的菜单:

@foreach(b_get_nav_items('primary_navigation') as $item)
... my html here...
@endforeach

在我的 foreach 中,我试图从我的菜单中调用我的 ACF 字段,如下所示:

<?php get_field('svg', $item->id)

但它不起作用(null)。我迷路了。如何获取我的 ACF 字段?

非常感谢

【问题讨论】:

  • 该字段是保存在菜单上还是菜单项上?
  • 看看你能不能得到你的领域var_dump(get_fields($item-&gt;id));也许?
  • 该字段保存在菜单项上,get_field($item) 不起作用(stdClass 类的对象无法转换为字符串)。感谢您的帮助
  • 是的,抱歉 - 你需要 id - 我编辑了它
  • 我试过 id)); ?>,但返回 false

标签: wordpress advanced-custom-fields


【解决方案1】:

in the ACF docs 没有记录,但如果您以“menu_”为前缀,这似乎有效。例如:

get_field('field_name', 'menu_' . $menu_id);

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 2019-01-13
    • 2019-11-03
    • 1970-01-01
    • 2017-02-01
    • 2019-04-11
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多