[英]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.