从数据库 table 动态填充 Wordpress ACF 中的 select 字段
Dynamically populate a select field in Wordpress ACF from a database table
是的,我对这个问题有很多批评,所以我会重写它。
我在 Wordpress 中使用高级自定义字段 (ACF) 来构建我的表单。我的特殊问题是,在 Select 中,我不想预填充该字段,而是在 运行 时间从数据库中的 table 动态填充它。
要构建表单组,您可以使用 ACF 的图形用户界面来创建和定义一个表单组,其中包含一个或多个表单字段。其中之一是定义您导出工作代码,然后在应用程序 运行s 时使用此代码。导出的代码是定义Form特性的数组和子数组的数组,不能包含动态代码。
顺便说一句,如果字段中的项目是 Post 类型,那么 ACF 允许您指向并按 Post 类型进行过滤,从而给出一种 Select 字段。但是我不想将此特定数据设为 Post 类型。
为硬连线 Select 字段创建的代码示例如下。
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 (
这是我希望动态替换的 'choices' 数组。
table 的示例和我希望使用的数据如下。
DROP TABLE IF EXISTS `counties `;
创建 TABLE counties
(
id
bigint(20) 无符号 NOT NULL AUTO_INCREMENT,
county
varchar(200) 不为空,
主键 (id
)
) 引擎=InnoDB AUTO_INCREMENT=91 默认字符集=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
它位于 http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
的 ACF 文档中
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');
是的,我对这个问题有很多批评,所以我会重写它。 我在 Wordpress 中使用高级自定义字段 (ACF) 来构建我的表单。我的特殊问题是,在 Select 中,我不想预填充该字段,而是在 运行 时间从数据库中的 table 动态填充它。
要构建表单组,您可以使用 ACF 的图形用户界面来创建和定义一个表单组,其中包含一个或多个表单字段。其中之一是定义您导出工作代码,然后在应用程序 运行s 时使用此代码。导出的代码是定义Form特性的数组和子数组的数组,不能包含动态代码。
顺便说一句,如果字段中的项目是 Post 类型,那么 ACF 允许您指向并按 Post 类型进行过滤,从而给出一种 Select 字段。但是我不想将此特定数据设为 Post 类型。
为硬连线 Select 字段创建的代码示例如下。
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 (
这是我希望动态替换的 'choices' 数组。
table 的示例和我希望使用的数据如下。
DROP TABLE IF EXISTS `counties `;
创建 TABLE counties
(
id
bigint(20) 无符号 NOT NULL AUTO_INCREMENT,
county
varchar(200) 不为空,
主键 (id
)
) 引擎=InnoDB AUTO_INCREMENT=91 默认字符集=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
它位于 http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
的 ACF 文档中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');