【问题标题】:Data Structure of Wordpress' get_posts function?Wordpress 的 get_posts 函数的数据结构?
【发布时间】:2011-01-01 18:17:03
【问题描述】:

我正在使用 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 ";

但是,我的 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 输出。任何建议将不胜感激。

【问题讨论】:

    标签: php wordpress


    【解决方案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

    【讨论】:

    • 我刚刚编辑了我的答案。该代码对我有用 - 我将它放入我个人博客上的 archive.php 中。并运行它。
    • 我的错。我刚刚意识到我正在通过尝试将其转换为数组来清除信息。它现在对我有用。谢谢!!
    • 不,您没有清除它 - 但它并没有达到您的预期。对象 $posts[0] 将是 $CurrentPost 数组的第一个元素。如果你想从 $CurrentPost 对象本身中检索它,你可以像这样得到它: $CurrentPost[0]->post_date)
    【解决方案2】:

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

    $Post_Date = $CurrentPost[post_date];
    

    使用:

    $Post_Date = $CurrentPost->post_date;
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多