在外部网站上按 ID 显示 WordPress 帖子?

use*_*431 0 php wordpress jquery

我知道如何从博客中提取最后 3 篇帖子并将其显示在外部网站上,但是是否可以指定帖子 ID 并将这些帖子显示在外部网站上?例如,如果我想在外部网站的主页上显示帖子 5、12 和 15?

小智 6

$url = 'YOUR_BLOG_URL';

// Get latest 3 posts
$latestPosts = file_get_contents($url . '/wp-json/wp/v2/posts?orderby=date&order=desc&per_page=3');

// Get 3 specific posts by ID
$postIds = array(5, 12, 15);
$specificPosts = file_get_contents($url . '/wp-json/wp/v2/posts?orderby=date&order=desc&include=' . implode(',', $postIds));
Run Code Online (Sandbox Code Playgroud)

以下是 REST API 文档的链接,以获取完整的参数列表:

https://developer.wordpress.org/rest-api/reference/posts/#arguments