API请求中没有特色媒体

时间:2019-04-25 作者:evgeni fotia

当我向http://localhost/wordpress/wp-json/wp/v2/posts/?_embed&categories=${categoryId}&per_page=${state.perPage}

我得到以下结果

[                                                                                                            
  {
    id: 3,
    date: \'2019-04-20T16:10:00\',
    date_gmt: \'2019-04-20T16:10:00\',
    guid: {
      rendered: \'http://localhost/wordpress/?p=3\'
    },
    modified: \'2019-04-20T16:36:30\',
    modified_gmt: \'2019-04-20T16:36:30\',
    slug: \'slug-title\',
    status: \'publish\',
    type: \'post\',
    link: \'http://localhost/wordpress/2019/04/20/slug-title/\',
    title: {
      rendered: \'title\'
    },
    content: {
      rendered: \'\\n<figure class="wp-block-image"><img \' +
        \'src="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg" \' +
        \'alt="" class="wp-image-4" \' +
        \'srcset="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg \' +
        \'370w, \' +
        \'http://localhost/wordpress/wp-content/uploads/2019/04/sirius-217x300.jpg \' +
        \'217w" sizes="(max-width: 370px) 100vw, 370px" \' +
        \'/></figure>\\n\',
      protected: false
    },
    excerpt: {
      rendered: \'\',
      protected: false
    },
    author: 1,
    featured_media: 0,
    comment_status: \'open\',
    ping_status: \'open\',
    sticky: false,
    template: \'\',
    format: \'standard\',
    meta: [],
    categories: [
      3
    ],
    tags: [],
    _links: {
      self: [Array],
      collection: [Array],
      about: [Array],
      author: [Array],
      replies: [Array],
      \'version-history\': [Array],
      \'predecessor-version\': [Array],
      \'wp:attachment\': [Array],
      \'wp:term\': [Array],
      curies: [Array]
    },
    _embedded: {
      author: [Array],
      \'wp:term\': [Array]
    }
  }
]
但正如您所看到的,没有特色的\\ U媒体。我是否对featured\\u media有误解?我的帖子里有一幅你在content.rendered

1 个回复
SO网友:Sobhan Askari

你没有为你的帖子设置特色图片,所以在特色图片obj中返回0。设置后,可以使用特色图像obj。

例如:我是用PHP写的,您可以将其上载到另一个主机,并显示您在这些主机上的URL变量中指定的WordPress网站的图像。

$url = \'url of your wordpress website\';
function Json_Data_Decoder($query){     
    global $url;
    $json = file_get_contents($url.\'/wp-json/wp/v2/\'. $query);
    $data = json_decode($json);
    return $data;
}
function Get_Media($post_media){
    global $url;
    $json = file_get_contents($url.\'/wp-json/wp/v2/media?include=\'.$post_media);
    $medias = json_decode($json);
    $featured_image = $medias[0]->media_details->sizes->medium->source_url;
    return $featured_image;
}

$posts = Json_Data_Decoder(\'posts\');
foreach ($posts as $post){
    
    // get media 
    $post_media = $post->featured_media;
    $featured_image = Get_media($post_media);
    
    echo 
    \'<article class="post image">
            <img src="\'.
                $featured_image .
            \'">
    </article>\';
}

相关推荐

WP-API:获取多个类别的帖子

比方说,我有两个类别,\'color\' 和\'temperature\'. 每一个都有许多子类别。当我转到如下URL时,Wordpress在显示请求类别中的帖子方面做得很好:http://www.example.com/category/pink+warm 现在我想对json-rest-api 插件,通过REST请求。有可能吗?如何使用REST请求获取多个类别的帖子(不是在任何一个类别中,而是在所有请求的类别中)?我尝试了不同的URI,但失败了。