我正在使用最新的Gutenberg和WP,直到上周,下面的代码仍按预期工作。
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});突然之间,我得到的wp.apiFetch不是函数错误。
有人知道为什么吗??
发布于 2018-11-28 20:31:37
正如@Alvero在注释中指出的那样,您现在需要指定wp,而不是仅仅在块注册中提供wp。
$index_js = 'sample-post/index.js';
wp_register_script(
'sample-post-block-block-editor',
plugins_url( $index_js, __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-api-fetch',
),
filemtime( "$dir/$index_js" )
);然后在您的块中,使用wp.apiFetch函数调用它:
var registerBlockType = wp.blocks.registerBlockType,
el = wp.element.createElement,
__ = wp.i18n.__,
apiFetch = wp.apiFetch;
const postSelections = [];
const allPosts = apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});https://wordpress.stackexchange.com/questions/320483
复制相似问题