[英]How to get the Field Group ID from a page using ACF and Wordress?
我创建了许多字段组,并在著名的高级自定义字段 (ACF) 插件中使用了 ' Show this field group if.. Page is equal to.. X '。
我正在使用get_post_custom_keys()
显示页面中的所有自定义字段:
$custom_field_keys = get_post_custom_keys(45);
我硬编码了“45”,它不是页面 ID,而是字段组 ID。 我正在努力获取与页面关联的字段组的 ID。
get_post_custom_keys($post_id);
将显示页面的自定义字段而不是字段组。
我知道一页可能有多个字段组。
这是我的解决方案。 我直接查看数据库,寻找当前页面的所有ACF规则,然后获取该ID。
//Look for ACF rules for the current post
$rows = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE meta_key = 'rule'");
foreach ($rows as $row) {
$values = unserialize($row->meta_value);
if ($postid == $values["value"]) { $numberofacffield = $row->post_id; }
}
//Then read fields for the acf group id : $numberofacffield
$custom_field_keys = get_post_custom_keys($numberofacffield);
从 ACF 5.0 开始,您可以使用此 function。
$post_id = get_the_ID();
$groups = acf_get_field_groups([
'post_id' => $post_id,
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.