2016-08-23 98 views
2

我使用WP REST API從我的網站上檢索數據,例如,來自此http:http://localhost:8888/wordpress/wp-json/wp/v2/posts/42我可以在後42的信息,但內容部分裏面,它顯示了這樣WordPress的REST API:如何獲得WP REST API JSON文件中的「僅字」內容?

enter image description here

實際崗位的格式爲:

這是一個測試博客+ [圖片] +這是一個測試博客+ [圖]

所有我從內容部分要的只是這個詞,而不是圖像的信息,我能做些什麼來實現這一目標?

以及WP REST API爲此內容部分返回的格式是什麼?我從網站上讀到它說它是「對象」。我對WP很陌生。

+0

這個問題你有答案嗎?如果是的話請更新它,我也遇到同樣的問題 –

回答

0

您需要登錄到rest_api_init並修改您需要的內容。

add_action('rest_api_init', function() 
{ 
    register_rest_field(
      'post', 
      'content', 
      array(
       'get_callback' => 'do_raw_shortcodes', 
       'update_callback' => null, 
       'schema'   => null, 
     ) 
     ); 
}); 

function do_raw_shortcodes($object, $field_name, $request) 
{ 
    global $post; 
    $post = get_post ($object['id']); 
    // This is what we currently have... 
    $output['rendered'] = apply_filters('the_content', $post->post_content); 
    // This includes the shortcodes 
    $output['_raw'] = $post->post_content; 
    // Add something custom 
    $output['foo'] = 'bar'; 
    return $output; 
} 

您應該看到JSON中的額外數據。