访问数组中的值/Object?
Accessing Values in an Array / Object?
正在尝试遍历一组类别并显示每个类别中最新 post 的标题。
$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);
此代码returns
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(); the_content();等等?
你应该重新写成这样:
$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();
}
希望对您有所帮助。
正在尝试遍历一组类别并显示每个类别中最新 post 的标题。
$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);
此代码returns
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(); the_content();等等?
你应该重新写成这样:
$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();
}
希望对您有所帮助。