2015-03-31 137 views
0

試圖遍歷一組類別並顯示最新帖子的標題。訪問數組/對象中的值?

$feed_sources = array('goose-creek','sleepy-creek','fobr'); 
    foreach ($feed_sources as $feed) { 
    $args = array('category_name' => $feed, 'posts_per_page' => 1); 
    $show = get_posts($args); 
    print_r($show); 

此代碼返回

Array ([0] => WP_Post Object ([ID] => 79 [post_author] => 1 [post_date] => 2015-03-19 08:58:40 [post_date_gmt] => 2015-03-19 09:58:40 [post_content] => 

但我沒有運氣在$顯示[0] [ 'POST_TITLE'],$秀[0] [POST_TITLE],或訪問它$顯示[0] - >'post_title'

此外,是否有一個簡單的方法來獲得此數組與基本主題功能,如the_title();內容();等等?

+1

使用$ show [0] - > post_title – 2015-03-31 17:34:31

+0

WP函數通常使用全局$ post變量。你可以設置它的價值,使他們工作。 – 2015-03-31 17:36:53

+0

不返回任何內容:( – 2015-03-31 17:36:56

回答

3

你應該把它重新寫成這樣:

$feed_sources = array('goose-creek','sleepy-creek','fobr'); 

foreach ($feed_sources as $feed) { 
    $args = array('category_name' => $feed, 'posts_per_page' => 1); 

    // we are creating new WP_Query object instead using get_posts() function 
    $shows = new WP_Query($args); 

    $shows->the_post(); 
    // now you can use the_title() and the_content() 
    the_title(); 
} 

希望這有助於。