WP REST API:最新帖子的详细信息,包括一个请求中的特色媒体URL?

时间:2016-10-02 作者:VSB

我正在使用wp-rest api 获取帖子信息。我还使用wp rest api filter items 要筛选字段并汇总结果,请执行以下操作:

当我打电话时http://example.com/wp-json/wp/v2/posts?items=id,title,featured_media 它返回如下结果:

[

    {
        "id": 407,
        "title": {
            "rendered": "Title 1"
        },
        "featured_media": 399
    },
    {
        "id": 403,
        "title": {
            "rendered": "Title 2"
        },
        "featured_media": 401
    }

]
问题是如何使用此id生成特色媒体url?默认调用http://example.com/wp-json/wp/v2/media/401 返回一个新的json,其中包含关于不同大小源图像的url的所有详细信息:

{

    "id": 401,
    "date": "2016-06-03T17:29:09",
    "date_gmt": "2016-06-03T17:29:09",
    "guid": {
        "rendered": "http://example.com/wp-content/uploads/my-image-name.png"
    },
    "modified": "2016-06-03T17:29:09",
    "modified_gmt": "2016-06-03T17:29:09",
    "slug": "my-image-name",
    "type": "attachment",
    "link": "http://example.com/my-post-url",
    "title": {
        "rendered": "my-image-name"
    },
    "author": 1,
    "comment_status": "open",
    "ping_status": "closed",
    "alt_text": "",
    "caption": "",
    "description": "",
    "media_type": "image",
    "mime_type": "image/png",
    "media_details": {
        "width": 550,
        "height": 250,
        "file": "my-image-name.png",
        "sizes": {
            "thumbnail": {
                "file": "my-image-name-150x150.png",
                "width": 150,
                "height": 150,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-150x150.png"
            },
            "medium": {
                "file": "my-image-name-300x136.png",
                "width": 300,
                "height": 136,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-300x136.png"
            },
            "one-paze-port-thumb": {
                "file": "my-image-name-363x250.png",
                "width": 363,
                "height": 250,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-363x250.png"
            },
            "one-paze-blog-thumb": {
                "file": "my-image-name-270x127.png",
                "width": 270,
                "height": 127,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-270x127.png"
            },
            "one-paze-team-thumb": {
                "file": "my-image-name-175x175.png",
                "width": 175,
                "height": 175,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-175x175.png"
            },
            "one-paze-testimonial-thumb": {
                "file": "my-image-name-79x79.png",
                "width": 79,
                "height": 79,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-79x79.png"
            },
            "one-paze-blog-medium-image": {
                "file": "my-image-name-380x250.png",
                "width": 380,
                "height": 250,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name-380x250.png"
            },
            "full": {
                "file": "my-image-name.png",
                "width": 550,
                "height": 250,
                "mime_type": "image/png",
                "source_url": "http://example.com/wp-content/uploads/my-image-name.png"
            }
        },
        "image_meta": {
            "aperture": "0",
            "credit": "",
            "camera": "",
            "caption": "",
            "created_timestamp": "0",
            "copyright": "",
            "focal_length": "0",
            "iso": "0",
            "shutter_speed": "0",
            "title": "",
            "orientation": "0",
            "keywords": [ ]
        }
    },
    "post": 284,
    "source_url": "http://example.com/wp-content/uploads/my-image-name.png",
    "_links": {
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/media/401"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/media"
            }
        ],
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/attachment"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/1"
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=401"
            }
        ]
    }

}
但是,当我想获取帖子列表及其缩略图时,请考虑这种情况。有一次我应该打电话http://example.com/wp-json/wp/v2/posts?items=id,title,featured_media 那我应该打电话http://example.com/wp-json/wp/v2/media/id 对每个媒体id执行10次,然后解析结果并获得媒体缩略图的最终url。所以它需要11个请求来获取10篇文章的详细信息(一个用于列表,10个用于缩略图)。是否可以在一个请求中获得此结果?

3 个回复
最合适的回答,由SO网友:StephanieQ 整理而成

啊,我自己也有这个问题!而与此同时_embed 很好,根据我的经验,它非常慢,JSON的要点是要快:D

我在插件中有以下代码(用于添加自定义帖子类型),但我想您可以将其放在主题中function.php 文件

php

add_action( \'rest_api_init\', \'add_thumbnail_to_JSON\' );
function add_thumbnail_to_JSON() {
//Add featured image
register_rest_field( 
    \'post\', // Where to add the field (Here, blog posts. Could be an array)
    \'featured_image_src\', // Name of new field (You can call this anything)
    array(
        \'get_callback\'    => \'get_image_src\',
        \'update_callback\' => null,
        \'schema\'          => null,
         )
    );
}

function get_image_src( $object, $field_name, $request ) {
  $feat_img_array = wp_get_attachment_image_src(
    $object[\'featured_media\'], // Image attachment ID
    \'thumbnail\',  // Size.  Ex. "thumbnail", "large", "full", etc..
    true // Whether the image should be treated as an icon.
  );
  return $feat_img_array[0];
}
现在,在JSON响应中,您应该看到一个名为"featured_image_src": 包含缩略图的url。

在此处阅读有关修改响应的更多信息:
http://v2.wp-api.org/extending/modifying/

以下是有关register_rest_fieldwp_get_attachment_image_src() 功能:
1。)https://developer.wordpress.org/reference/functions/register_rest_field/
2。)https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

**注意:不要忘记<?php ?> 标签,如果这是一个新的php文件!

SO网友:Jesús Franco

只需添加_embed URL的查询参数请求帖子和每个帖子对象,将包括_embedded.[wp:featuredmedia] 对象,它包括所有图像,就像/media/$id 资源如果需要特定的大小,只需通过其属性名称进行访问,即:_embedded[wp:featuredmedia][0].media_details.sizes.full.source_url 或其缩略图:_embedded[wp:featuredmedia][0].media_details.sizes.thumbnail.source_url

也就是说,wp:featuredmedia嵌入对象包括所有URL和帖子可用大小的详细信息,但如果您只需要原始特色图像,可以使用以下键中的值:post._embedded["wp:featuredmedia"][0].source_url

我在类似这样的网站中使用它(当然,使用您自己的域):

$.get(\'https://example.com/wp-json/wp/v2/posts/?categories=3&_embed\', 
    function(posts) { 
        var elems = \'\';
        posts.forEach(function(post){ 
            var link = post.link;
            var title = post.title.rendered;
            var pic = post._embedded["wp:featuredmedia"][0].source_url);
            elems += \'<div class="this_week"><a href="\' + link + \'" target="_blank">\';
            elems += \'<img src="\' + pic + \'" title="\' + title + \'"/><span class="title">\';
            elems += title + \'</span></a></div>\';
        });
        $(\'#blockbusters\').html(elems);
    });
});
看到了吗?不需要两个查询,只需添加_embed 作为查询参数,然后您就拥有了使用视图的最佳大小所需的所有信息。

SO网友:Idriss elbaz

添加(&A)_嵌入到您的URL会将wp:featuremedia添加到您的JSONE,即使您有自定义帖子,例如

https://example.com/wp-json/wp/v2/posts?search=Sometitlepost&_embed&order=asc