How to import featured image using WP Rest API from another WP installation

Question

First of all I’m designer, not programmer (…so here’s the real problem I guess : | )
I can’t see the way to import featured image using WP Rest API from installation #1 to another, when both are using same DB. I’ve been using next plugin:

    <?php
/**
 * Plugin Name:  Get Posts via REST API
 * Description:  Gets the latest two posts from a blog via the REST API. Blog link, title and date included.
 * Plugin URI:   https://renemorozowich.com
 * Author:       Rene Morozowich
 * Version:      1.0
 * Text Domain:  getpostsviarestapi
 * License:      GPL v2 or later
 * License URI:  https://www.gnu.org/licenses/gpl-2.0.txt
 *
 * @package getpostsviarestapi
 */

// Disable direct file access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Get posts via REST API.
 */
function get_posts_via_rest() {

    // Initialize variable.
    $allposts = '';
    
    // Enter the name of your blog here followed by /wp-json/wp/v2/posts and add filters like this one that limits the result to 2 posts.
    $response = wp_remote_get( 'https://www.heretheweb.com/blog/wp-json/wp/v2/posts?per_page=3&embed=true' );

    // Exit if error.
    if ( is_wp_error( $response ) ) {
        return;
    }

    // Get the body.
    $posts = json_decode( wp_remote_retrieve_body( $response ) );

    // Exit if nothing is returned.
    if ( empty( $posts ) ) {
        return;
    }

    // If there are posts.
    if ( ! empty( $posts ) ) {

        // For each post.
        foreach ( $posts as $post ) {

            // Use print_r($post); to get the details of the post and all available fields
            // Format the date.
            $fordate = date( 'n/j/Y', strtotime( $post->modified ) );
            
            // $featured_image = get_the_post_thumbnail();

            // Show a linked title and post date.
            // $allposts .= '<a href="' . esc_url( $post->link ) . '" target="_blank">' . esc_html( $post->title->rendered ) . '</a>  ' . esc_html( $fordate ) . '<br />';
            $allposts .= '<div class="item-blog-container"> <img src="' . esc_html( $featured_media ) . '"><a href="' . esc_url( $post->link ) . '" target="_blank">' . esc_html( $post->title->rendered ) . '</a> <br> <span style="font-size:0.8em; font-weight:bold;"> ' . esc_html( $fordate ) . '</span>  </div>';
        }
        
        return $allposts;
    }

}
// Register as a shortcode to be used on the site.
add_shortcode( 'sc_get_posts_via_rest', 'get_posts_via_rest' );

And now I have what I want, display title posts as links and date from WP installation#1 in #2. But I don’t know how to display featured images from posts at installation #1 into #2.

Can you help me, please?

Thanks in advance.

0
Backfolder 4 months 2020-12-21T14:10:27-05:00 0 Answers 4 views 0

Leave an answer

Browse
Browse