Wordpress REST API, GET featured image with a call

0

I'm making an android application for my wordpress page. I want to get several JSON data, one of them the image associated with the posts. The JSON that returns wordpress by default has the following format:

"author": 5,
"featured_media": 1836,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"meta":

With this format the GET only returns the file ID so I need to make another call to the service to get the url of the image associated with the post. How could I get the url of the image directly when I return a post from the page? Right now I'm making two calls and it's very inefficient and slow. I have tried to make changes to the plugin but it does not change the format. Any solution? I just need you to return the url of the image by doing a GET of the posts on the page.

public class FeedTask extends AsyncTask<String, Void, ArrayList<JSONArray>>{

private OnFeedListener listener;

public FeedTask(OnFeedListener listener){
    this.listener=listener;
}

@Override
protected ArrayList<JSONArray> doInBackground(String... params) {
    ArrayList<JSONArray> arrays=new ArrayList<>();
    String url=params[0];
    OkHttpClient client=new OkHttpClient();
    Request.Builder builder=new Request.Builder();
    Request request=builder.url(url).build();
    try {
        Response response=client.newCall(request).execute();
        String json=response.body().string();
       try{
           JSONArray array= new JSONArray(json);
           //Array de Imágenes
           JSONArray arrayImages=new JSONArray();
           for(int i=0; i<array.length(); i++) {
               String urlImage=("http://MIPAGINA/wp-json/wp/v2/media/")+array.optJSONObject(i).optString("featured_media");
               request=builder.url(urlImage).build();
               response=client.newCall(request).execute();
               json=response.body().string();
               arrayImages.put(new JSONObject(json));
           }
           arrays.add(array);
           arrays.add(arrayImages);
           return arrays;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(ArrayList<JSONArray> array) {
    super.onPostExecute(array);
    if(array==null){
        return;
    }
    if(listener!=null){
        listener.onFeed(array);
    }
}

}

    
asked by Francisco 28.11.2016 в 12:31
source

1 answer

0

Paste this into your functions.php

//Prepare REST
function prepare_rest($data, $post, $request){
  $_data = $data->data;
  //Thumbnails
  $thumbnail_id = get_post_thumbnail_id( $post->ID );
  //Acá puedes reemplazar el 'full' por el tamaño del thumbnail que hayas creado
  $thumbnailNews = wp_get_attachment_image_src( $thumbnail_id, 'full' );

  $_data['featured_news_image'] = $thumbnailNews[0];

  return $data;
}
add_filter('rest_prepare_post', 'prepare_rest', 10, 3);
    
answered by 08.07.2017 в 04:14