2015-03-31 99 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(); 
} 

希望这有助于。