i have two site. main site and blog site. both of them are wordpress. i want show my latest post from
blog site to main site. but I have issue to retrieve post thumbnail.
all thing is OK except Thumbnail.
wp_get_attachment_image_src(get_post_thumbnail_id($post['id']), 'thumbnail')
retrive 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;
}
Does anyone know where the problem is?
i have two site. main site and blog site. both of them are wordpress. i want show my latest post from
blog site to main site. but I have issue to retrieve post thumbnail.
all thing is OK except Thumbnail.