繁体   English   中英

访问数组/对象中的值?

[英]Accessing Values in an Array / Object?

尝试循环浏览一组类别并显示每个类别中最新文章的标题。

  $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();)一起工作? 内容(); 等等?

您应该将其重写为如下所示:

$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();
}

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM