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 );
以上代码是我在网上找的,应用后,也确实有wordpress文章中的图片,但是我文章中有多个图片,如何修改代码或者其他解决的办法
json显示下thumbnailurl字段中只有第一张图片的地址,但是没有第二张图片地址
本人菜鸟一枚,还请各位帮帮忙,谢谢!