访问数组/对象中的值?

时间:2015-03-31 17:33:44

标签: php wordpress wordpress-theming

尝试循环显示一组类别并显示每个类别的最新帖子的标题。

  $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] => 

但我没有用$ show [0] ['post_title'],$ show [0] [post_title]或$ show [0] - >'post_title'

访问它

此外,是否有一种简单的方法可以使此数组与基本主题函数(如the_title();内容();等?

1 个答案:

答案 0 :(得分: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();
}

希望这有帮助。