Catch the image of a WordPress post from an external file

4

I have the titles of my last posts, however I also need to search and display the images along with the title. This is a file outside the WordPress folder, a static page I did in HTML and CSS:

<?php

include('esenergy/wp-load.php'); // Blog path

// Get the last 5 posts
$recent_posts = wp_get_recent_posts(array(
  'numberposts' => 4,
  'category' => 0,
  'orderby' => 'post_date',
  'post_type' => 'post',
  'post_status' => 'publish'
));

// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
  echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';

I'm trying to do something like this:

    
asked by anonymous 27.01.2016 / 17:15

1 answer

1

I found it to be using the WP_Query class and the the_post_thumbnail :

<ul>
<?php
define( 'WP_USE_THEMES', false );
include('esenergy/wp-load.php'); // Blog path
function recentPosts() {
    $rPosts = new WP_Query();
    $rPosts->query('showposts=3');
        while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
            <li>
                <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
            </li>   
        <?php endwhile; 
    wp_reset_query();
}
?>
</ul>
<?php echo recentPosts(); ?>
    
27.01.2016 / 18:30