我有一个字段my-field
my-field的类型是Select
字段属性Allow multiple已启用。
字段的可用值为
如果我手动更新字段,然后调用函数
get_field( 'my-field', 'option' );我有
[
0 =>
[
'value' => 'value-1',
'label' => 'value-1',
],
1 =>
[
'value' => 'value-2',
'label' => 'value-2',
],
];尝试1
如果我试图更新字段-我用这种方式调用函数update_field
$update_result_multiple = update_field(
'my-field',
[
0 =>
[
'value' => 'value-1',
'label' => 'value-1',
],
1 =>
[
'value' => 'value-2',
'label' => 'value-2',
],
2 =>
[
'value' => 'value-2',
'label' => 'value-3',
],
],
'option'
);我得到了update_result_multiple是false,所有的值都没有被选中
企图2
如果我尝试这样做,则已成功地更新了选定的值。
$update_result = update_field(
'my-field',
[
'value' => 'value-1',
'label' => 'value-1',
],
'option'
);问题是
我无法通过设置选定的多个值来更新Select字段。
只能手动设置选定的多个值。但不能用update_field函数
问题是
如何为选择具有update_field函数的字段类型设置选定的多个值?
发布于 2020-07-21 01:38:10
我找到了解决这个问题的方法。
我用第三个参数来调用get_fields -value= false。
这给了我理解正确格式的线索。
那我就这样给update_field打电话了
$update_result_multiple = update_field(
'my-field',
['value-1', 'value-2', 'value-3'],
'option'
);并成功更新字段。
发布于 2020-07-21 06:51:46
或者你也可以用这种方法。
// Save a checkbox or select value.
$field_name = "my-field";
$value = array("value-1", "value-2", "value-3");
update_field( $field_name , $value, 'option' )有关更多信息,请查看the update_field documentation
https://stackoverflow.com/questions/63005766
复制相似问题