【发布时间】: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->id));也许? -
该字段保存在菜单项上,get_field($item) 不起作用(stdClass 类的对象无法转换为字符串)。感谢您的帮助
-
是的,抱歉 - 你需要 id - 我编辑了它
-
我试过 id)); ?>,但返回 false
标签: wordpress advanced-custom-fields