![](/img/trans.png)
[英]ACF Dynamically populate a select field’s choices - not completely working
[英]Dynamically populate a select field in Wordpress ACF from a database table
是的,我对此问题提出了很多批评,所以我将其重写。 我在Wordpress中使用高级自定义字段(ACF)来构建表单。 我的特别问题是,在Select i中,我不想预填充该字段,而是在运行时从数据库中的表动态填充它。
要构建表单组,请使用ACF的gui创建并定义一个表单组,该表单组中包含一个或多个表单字段。 其中一个定义是导出工作代码,然后在应用程序运行时使用此代码。 导出的代码是由数组和子数组组成的数组,这些数组和子数组定义窗体的特征,并且不能包含动态代码。
顺便说一句,如果字段中的项目是“过帐类型”,则ACF允许您指向并按“过帐类型”进行过滤,从而提供一种“选择”字段。 但是我不想将此特定数据设为“邮政类型”。
为硬连线选择字段创建的代码示例如下。
acf_add_local_field_group(array (
'key' => 'group_568d1e1d7e7fd',
'title' => 'Course Information',
'fields' => array (
array (
'key' => 'field_568d1e2d97b99',
'label' => 'Accrediting Body',
'name' => 'joltle_course_accrediting_body',
'type' => 'select',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
0 => '',
1093 => 'British Institute of Cleaning Science',
1094 => 'British Oxygen Corporation (BOC)',
1095 => 'CardianBCT',
1096 => 'Chartered Institute of Environmental Health',
1097 => 'Critical Care Institute Manchester',
),
'default_value' => '0',
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
'disabled' => 0,
'readonly' => 0,
),
array (
我希望动态替换的是“选择”数组。
我希望使用的表格和数据的示例如下。
DROP TABLE IF EXISTS `counties `;
创建表counties
( id
bigint(20)无符号NOT NULL AUTO_INCREMENT, county
varchar(200)NOT NULL,PRIMARY KEY( id
))ENGINE = InnoDB AUTO_INCREMENT = 91 DEFAULT CHARSET = utf8;
插入counties
( id
, county
)值(1,'Bath and North East Somerset'),(2,'Bedford'),(3,'Blackburn with Darwen'),(4,'Blackpool'),(5, 'Bournemouth'),(6,'Bracknell Forest'),(7,'Brighton&Hove');
xx
它位于ACF文档中, 网址为http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
function acf_some_field( $field ) {
//Change this to whatever data you are using.
$data_from_database = array('key1' => 'value1', 'key2' => 'value2');
$field['choices'] = array();
//Loop through whatever data you are using, and assign a key/value
foreach($data_from_database as $field_key => $field_value) {
$field['choices'][$field_key] = $field_value;
}
return $field;
}
add_filter('acf/load_field/name=what_you_need', 'acf_some_field');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.