WordPress Gutenberg:动态块不';t加载帖子列表
在教程“”中,解释了在Gutenberg块中加载最近帖子的列表,要使用的代码如下:WordPress Gutenberg:动态块不';t加载帖子列表,wordpress,wordpress-gutenberg,gutenberg-blocks,Wordpress,Wordpress Gutenberg,Gutenberg Blocks,在教程“”中,解释了在Gutenberg块中加载最近帖子的列表,要使用的代码如下: var el = wp.element.createElement, registerBlockType = wp.blocks.registerBlockType, withSelect = wp.data.withSelect; registerBlockType( 'my-plugin/latest-post', { ... edit: withSelect( funct
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
withSelect = wp.data.withSelect;
registerBlockType( 'my-plugin/latest-post', {
...
edit: withSelect( function( select ) {
return {
posts: select( 'core' ).getEntityRecords( 'postType', 'post' )
};
} )( function( props ) {
if ( ! props.posts ) {
return "Loading...";
}
if ( props.posts.length === 0 ) {
return "No posts";
}
var className = props.className;
var post = props.posts[ 0 ];
return el(
'a',
{ className: className, href: post.link },
post.title.rendered
);
} ),
...
} );
我试过这段代码,但块总是显示“正在加载…”
似乎props.posts
总是null
或undefined
并且查询从不返回任何内容
显然,使用WordPress内置的“最近的文章”块可以很好地工作,并且列表加载正确
检查时,似乎使用了完全相同的选择器(还使用了来自块本身配置的更多参数):
你知道会发生什么吗
如何调试此问题?您是否找到了解决此问题的方法?我也面临着同样的问题,不知道是什么原因造成的。
...
export default withSelect( ( select, props ) => {
const { postsToShow, order, orderBy, categories } = props.attributes;
const { getEntityRecords } = select( 'core' );
const latestPostsQuery = pickBy( {
categories,
order,
orderby: orderBy,
per_page: postsToShow,
}, ( value ) => ! isUndefined( value ) );
return {
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
};
} )( LatestPostsEdit );