2016-08-24 5 views
0

알려진 것처럼 WordPress의 나머지 API는 어떤 게시물의 Get 요청이 Imaging 용도의 featured_media 속성을 포함한 게시물을 제공하는 방식으로 작동합니다. 나는 100 개의 게시물을 얻을 필요가있는 상황에서 'wp-json/wp/v2/posts? per_page = 100'을 사용하여 쉽게 할 수 있으며 제대로 작동하지만 문제는 이미지를 가져와야한다는 것입니다. 각 게시물도. 이렇게하려면 각 게시물마다 각 추가 요청을 만들어 이미지를 가져와야합니다 .... 그래서 100 개의 게시물을 얻으려면 101 건의 요청을 보내야하며 리소스와 시간은 끔찍한 낭비입니다. 100 개의 요청을 저장하고 단일 요청으로 모든 작업을 수행하려면 게시 속성에 이미지 링크를 포함하려면 어떻게해야합니까? 감사합니다.wordpress Rest api 게시 속성의 이미지 링크

답변

1

REST API 응답 내용을 수정해야합니다. 에는 원하는 정보가 포함될 수 있습니다..

이 나는 ​​사용자 정의 필드 값을 포함하고 싶었 내 예 중 하나였다 :이

을 가지고,

{ 
    "id": 20, 
    "date": "2016-06-08T16:37:23", 
    "date_gmt": "2016-06-08T16:37:23", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&p=20" 
    }, 
    "modified": "2016-06-20T11:45:22", 
    "modified_gmt": "2016-06-20T11:45:22", 
    "slug": "aquarium-club", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/aquarium-club/", 
    "title": { 
     "rendered": "Aquarium Club" 
    }, 
    "content": { 
     "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh</p>\n<p>&nbsp;</p>\n<style>\n.gmap-iframe-container {\nmax-width: 100%;\n}\n.gmap-iframe {\nwidth: 100%;\nheight: 200px;\nmin-width:100%;\n}\n</style>\n<div class=\"gmap-iframe-container\">\n<iframe src=\"https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2695.565888030695!2d19.052103115616678!3d47.49836967917771!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4741dc402a04eee3%3A0x6869564cd433693c!2sAkv%C3%A1rium+Klub!5e0!3m2!1shu!2shu!4v1466147278469\" frameborder=\"0\" style=\"border:0\" allowfullscreen class=\"gmap-iframe\"></iframe></p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh &nbsp;</p>\n" 
    }, 
    "featured_media": 0, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/xBW7DglTDGs" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/20" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=20" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=20" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 

주의 사항 : 위의 코드는 응답을 수정

add_action('rest_api_init', 'slug_register_embed_youtube'); 
// "venue" is a custom post type I created using the WP Types plugin 
function slug_register_embed_youtube() { 
    // first register the field with WP REST API 
    register_rest_field('venue', 
     'meta_data', 
     array(
      'get_callback' => 'venue_get_meta', 
      'update_callback' => null, 
      'schema'   => null, 
     ) 
    ); 
} 

function venue_get_meta($post, $field_name, $request) { 
    // I wanted the value to appear in the response as "youtube_embed", 
    // and I wanted the "wpcf-youtube-embed" custom field's value there 
    $meta_data = array(
     'youtube_embed' => get_post_meta($post[ "id" ], 'wpcf-youtube-embed')[ 0 ], 
    ); 
    return $meta_data; 
} 

"meta_data": { 
    "youtube_embed": "https://www.youtube.com/embed/xBW7DglTDGs" 
}, 

"tags"배열 다음에.

은 현재 WP REST API를 V2의 응답을 수정하기위한 모든 정보를 찾을 수 있습니다 http://v2.wp-api.org/extending/modifying/

+0

그게 바로 내가 그 코드를 작성해야하는 위치 내가 원하는하지만? –

+0

functions.php에 넣습니다. 실제로 add_action()을 사용하면 파일을 호출 할 때 WP가 실행하는 "위치 순서"가 아니라 "함수 순서"에 실제로 배치됩니다. 페이지가 새로 고침 될 때마다 테마의 function.php가 호출되므로 모든 페이지 새로 고침 (물론 모든 REST API 쿼리)에 코드가 표시됩니다. –

+0

고맙습니다. –

관련 문제