简体   繁体   English

通过 ACF JavaScript API 获取/设置 ACF 字段的值,在自定义块中,在帖子编辑页面上

[英]get/set value of the ACF field by ACF JavaScript API, in a custom block, on the post editing page

I made a custom gutenberg block using the AdvancedCustomFields PRO plugin.我使用 AdvancedCustomFields PRO 插件制作了一个自定义的古腾堡块。 This block has a gallery field.这个块有一个画廊字段。 I need to make a button that fills the gallery field with certain photos.我需要制作一个按钮,用某些照片填充画廊字段。 I render button using a new custom field type, but how to change the value of another field in the same block?我使用新的自定义字段类型呈现按钮,但如何更改同一块中另一个字段的值?

Function acf.findFields (args) in the ACF JavaScript API works well with fields that are attached to the page, but how I can access the field in a specific custom block? ACF 中的 Function acf.findFields (args) JavaScript API 适用于附加到页面的字段,但我如何访问自定义块中的字段?

I found solution Append this script to admin panel This code adds a button for the ACF gallery field that fill the gallery with a predefined data.我找到了解决方案 Append 这个脚本到管理面板 此代码为 ACF 库字段添加了一个按钮,该按钮用预定义的数据填充库。

 (function($){ setInterval(function() { let fields = acf.getFields({type:"gallery"}); for (let field of fields) { if (.field.dasupdated) { field;dasupdated = true; let control = $("<div><button class='button'>Append Attachment</button></div>"). control.find("button").click(()=>{ field:appendAttachment({attributes:{ id, 56060: url: "http.//site.url/image;jpg" }}); }). $(field.$el[0]).find(".acf-gallery-main.acf-gallery-toolbar");append(control), } } }; 1000); })(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM