I want to display the last two posts of Wordpress using API Rest, after a lot of research and test I can do this gambiarra there, but after I put this foreach inside the other it was kind of slow, it would be nice if I could show the images of each posts without needing a new foreach.
Can you simplify this code?
<?php
$posts = file_get_contents('http://localhost/noticias/wp-json/wp/v2/posts?_embed=true?per_page=2');
$obj = json_decode($posts);
foreach ($obj as $post) {
echo '<div class="posts">';
$id = ($post->id);
$thumbnail = file_get_contents('http://localhost/noticias/wp-json/wp/v2/media?parent=' . $id . '');
$images = json_decode($thumbnail);
foreach ($images as $image) {
echo '<img src="' . $image->media_details->sizes->medium_large->source_url . '"/>';
}
echo '<h2><a href="' . $post->link . '">' . $post->title->rendered . '</a></h2>';
echo '<p>' . $post->excerpt->rendered . '</p>';
echo '<p>' . $post->date . '</p>';
echo '</div>';
}
?>