【发布时间】:2020-04-10 22:25:18
【问题描述】:
我觉得我想要完成的事情很简单:创建一个自定义的 WordPress Gutenberg 块,自动填充作者姓名和图像并且不可编辑。然后在前端帖子上渲染这个块。
我已经从很多不同的角度进行了研究,我认为我需要的是一个动态块
https://developer.wordpress.org/block-editor/tutorials/block-tutorial/creating-dynamic-blocks/
第一个问题是 wp.data.select("core/editor").getCurrentPostId() 不加载帖子 ID。但老实说,我不知道返回是否会有我想要的作者信息。非常感谢任何指导。
const post_id = wp.data.select("core/editor").getCurrentPostId();
var el = wp.element.createElement;
wp.blocks.registerBlockType('my-plugin/author-block', {
title: 'Author Footer',
icon: 'admin-users',
category: 'my-blocks',
attributes: {
// Nothing stored
},
edit: wp.data.withSelect( function(select){ // Get server stuff to pass to our block
return {
posts: select('core').getEntityRecords('postType', 'post', {per_page: 1, post_id})
};
}) (function(props) { // How our block renders in the editor
if ( ! props.posts ) {
return 'Loading...';
}
if ( props.posts.length === 0 ) {
return 'No posts';
}
var post = props.posts[0];
// Do stuff with the post like maybe get author name and image???
// ... Return elements to editor viewer
}), // End edit()
save: function(props) { // Save the block for rendering in frontend post
// ... Return saved elements
} // End save()
});
【问题讨论】:
-
试试这个并检查你是否获得了帖子 ID:wp.data.withSelect("core/editor").getCurrentPostId();
标签: javascript wordpress gutenberg-blocks