我對這個the_content
過濾器在JSON API中顯示自定義字段的過濾器有點迷惑。the_content過濾器將自定義字段添加到JSON響應
我使用這個插件http://wordpress.org/plugins/json-rest-api/從我的自定義帖子類型中獲得JSON響應。這些自定義帖子類型具有我必須在移動應用中顯示的自定義字段。
要實現這一點,我寫了這個代碼,使用the_content filter
代替原來的內容,只顯示自定義文章類型與HTML標籤:
add_filter('the_content', 'add_custom_post_fields_to_the_content');
function add_custom_post_fields_to_the_content($content){
global $post;
$custom_fields = get_post_custom($post->ID);
$content = '<img id="provider-logo" src="'.$custom_fields["wpcf-logo"][0].'" />';
$content = $content.'<img id="provider-image" src="'.$custom_fields["wpcf-fotos"][0].'" />';
$content = $content.'<h1 id="provider-name">'.$post->post_title.'</h1>';
$content = $content.'<p id="provider-address">'.$custom_fields["wpcf-direccion"][0].'</p>';
$content = $content.'<p id="provider-phone">'.$custom_fields["wpcf-phone"][0].'</p>';
$content = $content.'<p id="provider-facebook">'.$custom_fields["wpcf-facebook"][0].'</p>';
return $content;
}
所以,後來當我通過瀏覽器請求的信息,這是一個示例http://bride2be.com.mx/ceremonia/自定義字段顯示正常,但是當我請求JSON數據時,只顯示沒有自定義字段值的HTML。
這裏ISS一個例子:
http://bride2be.com.mx/wp-json.php/posts?type=ceremonia
我是小本丟了,有人能幫助我嗎?
掃描插件的鉤子,看起來不多,但這個可能有所幫助:'apply_filters('json_prepare _ {$ this-> type}',$ _post,$ post,$ context);' – brasofilo
我發現這個在插件代碼'content'=> apply_filters('the_content',$ post ['post_content'])中,所以我認爲插件實現了該過濾器。 – kentverger
我沒有安裝插件來幫助更多...檢查[this](http://wordpress.stackexchange.com/questions/145/how-do-you-debug-plugins) – brasofilo