最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to grab posts in Gutenberg Block?

programmeradmin5浏览0评论

Trying to get latest 10 posts to display in a Gutenberg plugin, but I can't figure out how to access the latest posts from the block.js. I know how to do it from PHP and it's easy, but this is new to me. Do I have to make a request to the REST API or is there a better practice?

Trying to get latest 10 posts to display in a Gutenberg plugin, but I can't figure out how to access the latest posts from the block.js. I know how to do it from PHP and it's easy, but this is new to me. Do I have to make a request to the REST API or is there a better practice?

Share Improve this question asked Jul 14, 2020 at 15:55 Stephen SabatiniStephen Sabatini 2363 silver badges13 bronze badges 1
  • You may also be able to use the wp.api developer.wordpress/rest-api/reference/posts in this case wp.api.posts, I'm about to test this myself – Caleb Jay Commented Oct 17, 2022 at 5:40
Add a comment  | 

1 Answer 1

Reset to default 3

You'll need to use the REST API. You can access the latest posts by calling:

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

Here is some more information on using the package - https://developer.wordpress/block-editor/packages/packages-api-fetch/

发布评论

评论列表(0)

  1. 暂无评论