简体   繁体   English

如何使用 ACF 和 Wordress 从页面获取字段组 ID?

[英]How to get the Field Group ID from a page using ACF and Wordress?

I created many field groups and used ' Show this field group if.. Page is equal to.. X ' with the famous Advanced Custom Fields (ACF) plugin.我创建了许多字段组,并在著名的高级自定义字段 (ACF) 插件中使用了 ' Show this field group if.. Page is equal to.. X '。

I'm using get_post_custom_keys() to show all the custom fields from a page:我正在使用get_post_custom_keys()显示页面中的所有自定义字段:

$custom_field_keys = get_post_custom_keys(45);

I hardcoded the '45' which is not the page ID, but the Field Group ID.我硬编码了“45”,它不是页面 ID,而是字段组 ID。 I'm struggling to get the ID of the field group associated with the page.我正在努力获取与页面关联的字段组的 ID。

get_post_custom_keys($post_id); will show the custom fields for the page and not the field group.将显示页面的自定义字段而不是字段组。

I understand there might be multiple field groups associated with one page.我知道一页可能有多个字段组。

Here is my solution. 这是我的解决方案。 I look into the database directly and look for any ACF rules for the current page, and grab that 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);

Since ACF 5.0 you can use this function.从 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM