使用如下函数将元素加载到常量中:
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/posts"}).then(posts => {
postSelections.push({label: "Select a Post", value: 0});
$.each( posts, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
然后使用postSelections作为元素“选项”。
el(
wp.components.SelectControl,
{
label: __(\'Select a Post\'),
help: \'Select a post to display as a banner.\',
options: postSelections,
value: selectedPost,
onChange: onChangePost
}
),