admin管理员组文章数量:1313001
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:
* Author: Rene Morozowich
* Version: 1.0
* Text Domain: getpostsviarestapi
* License: GPL v2 or later
* License URI: .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( ';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.
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
* Author: Rene Morozowich
* Version: 1.0
* Text Domain: getpostsviarestapi
* License: GPL v2 or later
* License URI: https://www.gnu/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/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.
Share Improve this question asked Dec 21, 2020 at 19:11 BackfolderBackfolder 133 bronze badges1 Answer
Reset to default 1This endpoint /wp/v2/posts
does not return featured image src url. It provides featured image id, that you can use to get image url by performing another REST request. Following function performs a rest request using media id and get image src url.
/**
* Get media url from WP REST API
*
* @param $media_id int
* @return string
*/
function get_image_src_via_rest( $media_id = 0 ) {
$media_id = (int) $media_id;
if ( $media_id < 1 ) {
return '';
}
$response = wp_remote_get( 'https://www.heretheweb/blog/wp-json/wp/v2/media/' . $media_id );
// Exit if error.
if ( is_wp_error( $response ) ) {
return '';
}
// Get the body.
$media = json_decode( wp_remote_retrieve_body( $response ) );
return $media->source_url;
}
Now, inside your get_posts_via_rest
function, you can call this new function to grab the featured image url.
$featured_image = get_image_src_via_rest( $post->featured_media );
本文标签: pluginsHow to import featured image using WP Rest API from another WP installation
版权声明:本文标题:plugins - How to import featured image using WP Rest API from another WP installation 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741898497a2403724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论