2012-12-28 15 views
0

這是我打印出來的。如何通過JSON使用get_posts發送標題和固定鏈接

$args = array(
'category' => 'Animation', 
'numberposts' => 8 
); 
$posts_array = get_posts($args); 
echo json_encode($posts_array); 

這是結果:(有8名相同的JSON列表這樣的,只是示出了1爲了方便起見。)

{ 「ID」:554, 「post_author」: 「1」,「POST_DATE 「:」2012-12-28 19:17:43「,」post_date_gmt「:」2012-12-28 19:17:43「,」post_content「:」太空競賽已經開始。他的結果可能會對全人類更好的影響......他將迎接挑戰「,」post_title「:」Alpha「,」post_excerpt「:這是一個單一的黑猩猩的進展,這個黑猩猩被招募到太空計劃中。 「」, 「post_status」: 「發佈」, 「comment_status」: 「開放」, 「ping_status」: 「開放」, 「post_password」: 「」, 「POST_NAME」: 「阿爾法」, 「to_ping」: 「」,」 pinged「:」「,」post_modified「:」2012-12-28 19:17:43「,」 post_modified_gmt「:」2012-12-28 19:17:43「,」post_content_filtered「:」「,」post_parent「:0,」guid「:」http://localhost/Wordpress%203.4.2/?p = 554 「,」menu_order「:0,」post_type「:」post「,」post_mime_type「:」「,」comment_count「:」0「,」filter「:」raw「

但我只想發送id和post_content。我一直得到空值,不知道爲什麼。

回答

1

由你需要的屬性剛剛過濾器:(id and the post_contenttitle and permalink

$args = array(
    'category'  => 'Animation', 
    'numberposts' => 8 
); 

$posts_array = get_posts($args); 

$send_array = array(); 
foreach ($posts_array as $key => $value) 
{ 
    $send_array[$key]["ID"]   = $value->ID; 
    $send_array[$key]["post_content"] = $value->post_content; 
} 

echo json_encode($send_array); 
exit; 
相關問題