通过WP REST API获取子站文章缩略图失败问题求助
问题:跨WordPress站点展示博客站文章时,缩略图无法获取
我有主站与博客站两个WordPress站点,希望在主站展示博客站的最新文章。目前除文章缩略图外,其余内容均可正常获取,但调用wp_get_attachment_image_src(get_post_thumbnail_id($post['id']), 'thumbnail')返回false。以下是我使用的代码:
function display_latest_blog_posts() { ob_start(); // Start output buffering // Replace with the URL of your blog site $blog_site_url = 'http://www.blog.domain.com'; // Get the latest blog posts using WP REST API $response = wp_remote_get($blog_site_url . '/wp-json/wp/v2/posts?per_page=3'); $posts = wp_remote_retrieve_body($response); if (!empty($posts)) { $posts = json_decode($posts, true); echo '<div class="latest-blog-posts">'; foreach ($posts as $post) { echo '<div class="post">'; echo '<h2 class="post-title">' . $post['title']['rendered'] . '</h2>'; echo '<p class="post-date">' . date('F j, Y', strtotime($post['date'])) . '</p>'; if (has_post_thumbnail($post['id'])) { $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post['id']), 'thumbnail')[0]; echo '<img src="' . $thumbnail_url . '" class="post-thumbnail" alt="' . $post['title']['rendered'] . '">'; } echo '</div>'; } echo '</div>'; } $output = ob_get_clean(); // Get the buffered content return $output; }
解决方法
问题核心在于你用的has_post_thumbnail、get_post_thumbnail_id、wp_get_attachment_image_src都是主站本地的WordPress函数,这些函数只能读取主站数据库里的内容,而博客站的文章ID在主站数据库中没有对应的附件记录,自然返回false。
正确的做法是让博客站的REST API返回缩略图的URL,具体步骤如下:
- 修改API请求地址:在请求URL末尾添加
&_embed参数,让接口返回包含特色图片的嵌入数据。默认的WP REST API posts接口不会主动返回缩略图信息,_embed参数会触发返回相关的媒体数据。 - 从返回结果中提取缩略图URL:通过
$post['_embedded']['wp:featuredmedia'][0]['source_url']获取原图地址,如果需要指定尺寸(比如thumbnail),可以用$post['_embedded']['wp:featuredmedia'][0]['media_details']['sizes']['thumbnail']['source_url']。
修改后的完整代码:
function display_latest_blog_posts() { ob_start(); // Start output buffering // Replace with the URL of your blog site $blog_site_url = 'http://www.blog.domain.com'; // 添加&_embed参数获取缩略图嵌入数据 $response = wp_remote_get($blog_site_url . '/wp-json/wp/v2/posts?per_page=3&_embed'); $posts = wp_remote_retrieve_body($response); if (!empty($posts)) { $posts = json_decode($posts, true); echo '<div class="latest-blog-posts">'; foreach ($posts as $post) { echo '<div class="post">'; echo '<h2 class="post-title">' . $post['title']['rendered'] . '</h2>'; echo '<p class="post-date">' . date('F j, Y', strtotime($post['date'])) . '</p>'; // 检查是否存在嵌入的特色图片数据 if (isset($post['_embedded']['wp:featuredmedia'][0])) { // 获取thumbnail尺寸的图片URL,若不存在则用原图 $thumbnail_size = $post['_embedded']['wp:featuredmedia'][0]['media_details']['sizes']['thumbnail'] ?? null; $thumbnail_url = $thumbnail_size ? $thumbnail_size['source_url'] : $post['_embedded']['wp:featuredmedia'][0]['source_url']; echo '<img src="' . esc_url($thumbnail_url) . '" class="post-thumbnail" alt="' . esc_attr($post['title']['rendered']) . '">'; } echo '</div>'; } echo '</div>'; } $output = ob_get_clean(); // Get the buffered content return $output; }
额外说明:
- 如果你不想用
_embed参数,也可以在博客站的functions.php中添加代码,让REST API直接返回特色图片的URL字段,不过_embed是更简便的原生方案。 - 记得用
esc_url和esc_attr做安全转义,避免XSS风险。
内容的提问来源于stack exchange,提问作者mohsen hasanpoor
相关产品推荐
相关产品推荐

