2016-11-27 48 views
0

我使用WP REST API 2.0進行REST API支持。如何獲得wordpress響應的_embedded屬性附件?我通過了_embed參數但我沒有得到wp:attachment的對象。全網址:/wp-json/wp/v2/posts?_embed如何在WordPress REST響應中添​​加嵌入附件?

enter image description here

我期待的反應,就像this

enter image description here

+0

您需要提供更多關於您所嘗試的信息。請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – MikeJRamsey56

+0

我使用最新版本的Wordpress和'WP REST API 2.0'插件而無需修改。它更清楚嗎? – blits

回答

0

您可以添加該特定行動register_rest_field功能。

add_action('rest_api_init', function(){register_rest_field('your_post_type', 'field_to_show_in_response', array('get_callback' => 'func_to_get_meta_data', 'update_callback' => null, 'schema' => null));}); 

現在您的func_to_get_meta_data你必須調用get_attached_media例如,對於所有媒體。

function func_to_get_meta_data($obj, $name, $request){return get_attached_media('image', $obj['id']);} 

在這個例子中,我得到所有圖像附加到帖子或自定義帖子。