Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Wordpress REST API获取帖子缩略图_Wordpress_Api_Rest_Post - Fatal编程技术网

使用Wordpress REST API获取帖子缩略图

使用Wordpress REST API获取帖子缩略图,wordpress,api,rest,post,Wordpress,Api,Rest,Post,我使用这段代码通过Wordpress REST API获取帖子,这段代码非常有效,并且能够很好地获取帖子,但我不知道如何通过这段代码获取帖子缩略图。我在wp_remote_get函数中添加了?_嵌入到URL的末尾,并且输出了缩略图地址,但我不知道如何在php中获取和显示图像URL <?php function get_posts_via_rest() { // Initialize variable. $allposts = ''; // Enter

我使用这段代码通过Wordpress REST API获取帖子,这段代码非常有效,并且能够很好地获取帖子,但我不知道如何通过这段代码获取帖子缩略图。我在wp_remote_get函数中添加了?_嵌入到URL的末尾,并且输出了缩略图地址,但我不知道如何在php中获取和显示图像URL

<?php

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://video.amniat98.com/wp-json/wp/v2/posts?_embed' );

    // 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
            //print_r($post);
            // Show a linked title and post date.
            $allposts .= '<a href="' . esc_url( $post->link ) . '" target=\"_blank\">' . esc_html( $post->title->rendered ) . '</a>';
        }
        
        return $allposts;
    }

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

您要查找的
有帖子缩略图
帖子缩略图
帖子缩略图url


例子


了解更多

您要查找的
有\u post\u缩略图
有\u post\u缩略图
有\u post\u缩略图


例子


了解更多

此解决方案不起作用,我从另一个带有json的Wordpress站点收到帖子,我需要代码来显示帖子图片、标题和链接。此解决方案不起作用,我从另一个带有json的Wordpress站点收到帖子,我需要代码来显示帖子图片、标题和链接
<?php
/**
* The Loop
* @link https://developer.wordpress.org/themes/basics/the-loop/
*/
if ( have_posts() ):
    while ( have_posts() ): the_post();

      /**
      * has_post_thumbnail()
      * @link https://developer.wordpress.org/reference/functions/has_post_thumbnail/
      */
      if ( has_post_thumbnail() ):
        the_post_thumbnail();

        /**
        * get_the_post_thumbnail_url()
        * @link https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
        */
        the_post_thumbnail_url();
      endif;
    endwhile;
endif; ?>