我有一些定制的帖子列出汽车,摩托车和卡车。
ACF文件中的值是自动填充的,我不能手动更改它们,因为它们来自外部软件。
在这个CPT中,我有两个ACF字段(都是文本字段):
其中一种叫做“类别”,可以是汽车,摩托车,卡车。
另一个叫做“服务”的值,可以是1到9之间的值。
如您所见,“对象”可以有不同的类别,但它们可能共享相同的“服务”。
例如:
类别=汽车服务=9
类别=卡车服务=9
我想根据“类别”中的值更新ACF字段“Service”的值。
例如:
如果类别为“汽车”,而服务为9,则应在“租车”中注明9
如果类别为“卡车”,服务为9,则应在“购买”中添加9
我怎样才能做到这一点?
发布于 2022-04-26 11:41:42
你好,您可以使用下面的代码
function UpdateServiceField($post_id){
$category_field = get_field('category',$post_id);
$service_field = get_field('service',$post_id);
if ($category_field ==9) {
update_field('service','9',$post_id);
}
}
add_action('acf/save_post', 'UpdateServiceField', 20); 发布于 2022-04-27 09:23:50
这里是一个更完整的例子,您想要根据组合框的选择来更改某些字段的内容。这里JS部分:jQuery(文档).ready(($) => {acf.addFilter(‘select2 2_ajax_data“),(data,args,$input,field,=> )=>{
// Get the combo field instance
let your_combo = acf.getFields({
name: 'your_combo_field_name'
})[0];
if (your_combo !== undefined && (data.field_key === field.get('key')) {
data.your_value = your_combo.val(); // Here is the magic ;)
}
}
});})
https://stackoverflow.com/questions/72012922
复制相似问题