在主题下面的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 );