Wordpress 在自定义菜单中获取自定义字段ACF
我在Wordpress中有一个菜单,带有一个名为“svg”的ACF字段 要创建菜单,请执行以下操作:Wordpress 在自定义菜单中获取自定义字段ACF,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我在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
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
<?php get_field('svg', $item->id)
在我的foreach中,我试图从菜单中调用我的ACF字段,如下所示:
@foreach(b_get_nav_items('primary_navigation') as $item)
... my html here...
@endforeach
<?php get_field('svg', $item->id)
它没有文档记录,但如果您在前面加上“menu\uux”,这似乎是可行的。例如:
get_字段('field_name'、'menu_'.$menu\u id)代码>它没有文档记录,但如果您在前面加上“menu\ux”前缀,这似乎是可行的。例如:
get_字段('field_name'、'menu_'.$menu\u id)代码>字段是保存在菜单上还是保存在菜单项上?查看是否可以获取字段var\u转储(获取字段($item->id))代码>可能?该字段保存在菜单项上,get_字段($item)不起作用(stdClass类的对象无法转换为字符串)。谢谢你的帮助是的对不起-你需要id-我编辑了我试过的,但是返回False字段保存在菜单上还是菜单项上?看看你是否可以得到你的字段var_dump(get_字段($item->id))代码>可能?该字段保存在菜单项上,get_字段($item)不起作用(stdClass类的对象无法转换为字符串)。谢谢你的帮助是的抱歉-你需要id-我编辑了我尝试过的,但返回false