在前端显示当前用户数据 - 古腾堡块

发布于 2025-01-12 15:03:44 字数 333 浏览 1 评论 0原文

我创建了一个古腾堡块来显示当前用户数据,但它仅显示管理数据,因为当我保存帖子时,当前用户是管理员,因此它仅显示所有用户的管理数据,但我想要当前用户详细信息。

我已经尝试过这段代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以心动 2025-01-19 15:03:44

使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文