在前端显示当前用户数据 - 古腾堡块
我创建了一个古腾堡块来显示当前用户数据,但它仅显示管理数据,因为当我保存帖子时,当前用户是管理员,因此它仅显示所有用户的管理数据,但我想要当前用户详细信息。
我已经尝试过这段代码。
let current_user = wp.data.select("core").getCurrentUser();
console.log("current_user", current_user.id);
请让我知道如何在古腾堡块中显示当前用户元字段。 提前致谢。
注意:在我的例子中没有使用服务器端渲染,因为我想要这个块的丰富编辑器。
I have created a Gutenberg block for display current user data but its display only admin data because when I save post that's time current user is admin so its display only admin data for all user but I want current user detail.
I have try this code.
let current_user = wp.data.select("core").getCurrentUser();
console.log("current_user", current_user.id);
Please let me know how can I display current user meta fields in Gutenberg block.
Thanks in advance.
Note : Serverside render is not used in my case because I want rich editor for this block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
wp.data.select("core").getCurrentUser()
,只能检索古腾堡块编辑器上当前的编辑用户,保存信息时,会保存编辑用户的数据作为静态数据。所以你总是会在前端得到相同的数据。您必须使用 动态块< /a> 满足您的要求。
在 JS 上仅使用 edit 功能。
对于保存功能,请使用 PHP。
您可以查看 来自核心古腾堡的帖子标题块Block 是一个现代的例子。
Using
wp.data.select("core").getCurrentUser()
, you can only retrieve the current editing user on Gutenberg Block Editor, when you save the information, the data of the editing user is saved as static data. So you will always get the same data on the front-end.You have to use dynamic blocks for your requirements.
On JS use the edit function only.
For the save function use PHP.
You can view Post Title Block from the core Gutenberg Block for a modern example.