Wordpress'get_posts函数的数据结构?

时间:2011-01-01 18:17:03

标签: php wordpress

我正在使用Wordpress,并尝试从当天创建的所有帖子中获取信息。我的代码目前看起来像这样:

        $today = getdate();
 $posts = get_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );

 $CurrentPost = array($posts[0]);

 $Post_Date = $CurrentPost[post_date];
 echo "Post Date: $Post_Date ";

但是,我的发布日期为NULL。我是否正确访问了数据结构?我知道那里有数据,因为当我在$ posts数组上执行print_r时,我得到以下内容:

Array ( [0] => stdClass Object ( [ID] => 4 [post_author] => 1 [post_date] => 2011-01-01 02:59:09 [post_date_gmt] => 2011-01-01 02:59:09 [post_content] => This Post created Friday December 31st, 2010 [post_title] => Test Post 1 [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => test-post-1 [to_ping] => [pinged] => [post_modified] => 2011-01-01 02:59:09 [post_modified_gmt] => 2011-01-01 02:59:09 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=4 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) [1] => stdClass Object ( [ID] => 1 [post_author] => 1 [post_date] => 2011-01-01 02:43:02 [post_date_gmt] => 2011-01-01 02:43:02 [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! [post_title] => Hello world! [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => hello-world [to_ping] => [pinged] => [post_modified] => 2011-01-01 02:43:02 [post_modified_gmt] => 2011-01-01 02:43:02 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wordpress/?p=1 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 1 [filter] => raw ) )

我希望能够直接访问每个帖子中的每个元素,就像从数组中调用它一样,这样我就可以生成当天写的帖子的非html输出。任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

编辑: 所以我尝试了这个。这两个都适合我:


echo "Post Date 1: " .$posts[0]->post_date . "<br/>";
echo "Post Date 2: " . mysql2date('M j Y', $posts[0]->post_date);

这是我的输出:


Post Date 1: 2011-01-01 01:45:40
Post Date 2: Jan 1 2011


答案 1 :(得分:0)

当它是一个对象时,你试图以数组的形式访问数据。 而不是:

$Post_Date = $CurrentPost[post_date];

使用:

$Post_Date = $CurrentPost->post_date;