Blogger Information
Blog 34
fans 1
comment 0
visits 57154
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
利用WP REST API接口实现缩略图
Y的博客
Original
1674 people have browsed it

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


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post