|
能找着这儿就是缘分,这问题困扰我半小时。。。因为对WP不熟悉,用的少
最后和官方API地址对比发现缺少了 thumbnailurl 这个字段值
如果你用的是自己的主题,可能是不显示的
需要修改fucntion.php文件
方法如下:
在主题下面的functions.php下面添加
- function dw_rest_prepare_post( $data, $post, $request ) {
- $_data = $data->data;
- $params = $request->get_params();
- if (has_post_thumbnail($data)) {
- //如果有特色缩略图,则输出缩略图地址
- $image = wp_get_attachment_image_src( get_post_thumbnail_id($_data['id']), 'full' );
- $post_thumbnail_src = $image[0];
- } else {
- @$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
- if (!empty($matches[1][0])) {
- global $wpdb;
- $att = $wpdb->get_row($wpdb->prepare("SELECT ID,guid FROM $wpdb->posts WHERE guid LIKE '%s'", $matches[1][0]));
- if ($att) {
- $post_thumbnail_src = $att->guid;
- } else {
- $post_thumbnail_src = $matches[1][0];
- }
- } else {
- // 默认图片路径
- $post_thumbnail_src = null;
- }
- }
- $_data['thumbnailurl'] = $post_thumbnail_src;
- // if ( ! isset( $params['id'] ) ) {
- unset( $_data['excerpt'] );
- unset( $_data['author'] );
- unset( $_data['featured_media'] );
- unset( $_data['format'] );
- unset( $_data['ping_status'] );
- unset( $_data['comment_status'] );
- unset( $_data['sticky'] );
- unset( $_data['template'] );
- // }
- $data->data = $_data;
- return $data;
- }
- add_filter( 'rest_prepare_post', 'dw_rest_prepare_post', 10, 3 );
复制代码 |
|