通过字段名称以编程方式获取ACF字段密钥 [英] Get ACF field key programmatically by field name

查看:67
本文介绍了通过字段名称以编程方式获取ACF字段密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在WordPress的高级自定义字段插件(ACF)中按字段名称获取字段关键字。

I need to get the field key by field name in WordPress in the Advanced custom fields plugin (ACF).

该字段已分配给帖子。我处于发布循环中,我想使用字段名称以编程方式获取字段密钥。这样做的原因是因为我正在创建一个表单,其中字段名 options将保持不变,但在选择中将具有不同的选项。

The field is assigned to a post. I am in the loop of the post and I want to get the field key programmatically USING field name. The reason for this is because i'm creating a form where the field name 'options' will stay the same but will have different options in a select.

我正在循环在自定义帖子类型上并期望将字段选项分配给帖子(每个帖子将具有唯一的选项,因此将具有唯一的字段键,所以我不能仅使用字段键,因为这将是硬编码的到潜在的另一个帖子选项)...

I am looping over a custom post type and expecting the field 'options' to be assigned to the post (each post will have unique options and so will have a unique field key so i can't just use the field key as this would be hard-coded to potentially another posts options)...

我在帖子的循环中,该循环应包含自定义字段 options,因此我应该能够查找

I'm in the loop of the post which should contain the custom field 'options' so I should be able to look for field key using post id and custom field name?

编辑:
我发现了这个: https://gist.github.com/mcguffin/81509c36a4a28d9c682e

但似乎没有

推荐答案

正确的方法是使用 acf_maybe_get_field 函数,像这样:

The right way is to use acf_maybe_get_field function, just like that:

a cf_maybe_get_field('field_name',false,false);

参数为:字段名称帖子ID (默认为当前帖子)和最重要的 strict 默认为 true ,但是我们在此处将其设置为 false ,即使帖子中尚不存在该对象

The arguments are: field name, post id (defaults to current post) and the most important strict which defaults to true, but we set it to false here to get field object even when it not yet exists for the post.

这篇关于通过字段名称以编程方式获取ACF字段密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆