如何从另一个高级自定义字段获取高级自定义字段选择值?
How to get Advanced Custom Field choice value from another Advance Custom Field?
I have created two custom fields using Advanced Custom Field plugin. I want it to behave like when I insert value in one text field, it should be filled in the another field which is a choice field.
This image is an example where I am inserting Name value and slug value.
I want it to return this.
使用以下内容创建一个 javascript 文件(将两个 id 替换为字段的 id,如果我是对的,它应该以 acf-field- 开头)
(function($) {
$('#id_of_input_with_value').on('blur', function(){
$('#id_of_input_which_should_get_input_value').val($(this).val());
});
})(jQuery);
在您的 functions.php 中,确保 js 文件已加载到管理区域(在此示例中,link-acf-fields.js 文件位于我主题中的 js 文件夹中):
function my_custom_admin_scripts() {
wp_register_script('link-acf-fields', get_template_directory_uri() . '/js/link-acf-fields.js', 'jquery', false, true );
wp_enqueue_script('link-acf-fields');
}
add_action( 'admin_enqueue_scripts', 'my_custom_admin_scripts' );
要在 ACF 中自动填充 select 字段,您可以使用 load_field 函数 - 在此处查看更多信息:http://www.advancedcustomfields.com/resources/acfload_field/
所以,假设您的 select 字段名称是 marke_name,您可以将以下内容添加到 functions.php 文件中,这将每次为您填写字段
function acf_load_marke_name_field_choices($field)
{
global $post;
//Get the repeater field values
$choices = get_field('repeater_field_name',$post->ID);
// loop through array and add to field 'choices'
if (is_array($choices)) {
foreach ($choices as $choice) {
//Set the select values
$field['choices'][$choice['slug']] = $choice['name'];
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=marke_name', 'acf_load_marke_name_field_choices');
I have created two custom fields using Advanced Custom Field plugin. I want it to behave like when I insert value in one text field, it should be filled in the another field which is a choice field.
This image is an example where I am inserting Name value and slug value. I want it to return this.
使用以下内容创建一个 javascript 文件(将两个 id 替换为字段的 id,如果我是对的,它应该以 acf-field- 开头)
(function($) {
$('#id_of_input_with_value').on('blur', function(){
$('#id_of_input_which_should_get_input_value').val($(this).val());
});
})(jQuery);
在您的 functions.php 中,确保 js 文件已加载到管理区域(在此示例中,link-acf-fields.js 文件位于我主题中的 js 文件夹中):
function my_custom_admin_scripts() {
wp_register_script('link-acf-fields', get_template_directory_uri() . '/js/link-acf-fields.js', 'jquery', false, true );
wp_enqueue_script('link-acf-fields');
}
add_action( 'admin_enqueue_scripts', 'my_custom_admin_scripts' );
要在 ACF 中自动填充 select 字段,您可以使用 load_field 函数 - 在此处查看更多信息:http://www.advancedcustomfields.com/resources/acfload_field/
所以,假设您的 select 字段名称是 marke_name,您可以将以下内容添加到 functions.php 文件中,这将每次为您填写字段
function acf_load_marke_name_field_choices($field)
{
global $post;
//Get the repeater field values
$choices = get_field('repeater_field_name',$post->ID);
// loop through array and add to field 'choices'
if (is_array($choices)) {
foreach ($choices as $choice) {
//Set the select values
$field['choices'][$choice['slug']] = $choice['name'];
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=marke_name', 'acf_load_marke_name_field_choices');