简体   繁体   English

WordPress的古腾堡自定义块获得所有职位

[英]WordPress Gutenberg Custom Block Getting All Posts

I'm trying to create a custom block but I need some assistance. 我正在尝试创建一个自定义块,但是我需要一些帮助。

I'm attempting to setup a <select> in the editor that lists all the posts. 我正在尝试在列出所有帖子的编辑器中设置<select> I'm not very experienced at the api yet but after reading the docs, it seems that the below line should work to get the posts. 我在api上还没有很丰富的经验,但是在阅读了文档之后,下面的一行似乎应该可以用来获取帖子。

wp.api.collections.Posts().fetch()

The problem is, however, I'm getting a nice error of: 问题是,但是,我得到一个很好的错误:

TypeError: Cannot read property 'collections' of undefined TypeError:无法读取未定义的属性“ collections”

How can I get a list of all the posts for selection within the editor, or is it just outside the realm of possibility? 如何在编辑器中获取所有帖子的列表以供选择,或者仅在可能性范围之外?

Try using this instead 尝试使用它代替

const { apiFetch } = wp;
apiFetch({ path: "/wp/v2/posts" }).then(posts => {
  console.log(posts);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM