【问题标题】:Getting post author and date in Wordpress在 Wordpress 中获取帖子作者和日期
【发布时间】:2017-04-12 22:54:45
【问题描述】:

我正在使用以下代码在 Wordpress 中获取帖子信息。我在网上找到了这段代码,并添加了两个变量“日期”和“作者”。但是,这些目前没有按要求/预期工作。

对于“日期”,它返回完整的日期/时间戳(即 2017-03-08 15:12:39),而不是使用 get_the_date() 时检索到的格式良好的日期; (即 2017 年 3 月 8 日)。我可以调整我的标记来格式化日期吗?

对于“作者”,什么都没有返回?

提前致谢。

$postData = get_posts($postParameters);
$posts = array();

foreach($postData as $post) {
    $post = array(
        "id"       => $post->ID,
        "title"    => $post->post_title,
        "excerpt"  => $post->post_excerpt,
        "date"     => $post->post_date,
        "author"   => $post->post_author,
        "slug"     => $post->slug,
        "image"    => array("url" => get_the_post_thumbnail_url($post->ID)),
        "link"     => get_permalink($post->ID),
        "category" => get_the_category($post->ID)[0]->name
    );

    array_push($posts, $post);
}

【问题讨论】:

    标签: php wordpress date author


    【解决方案1】:

    这是因为您正在检索一个对象,在收到数据后您需要按照您需要的方式对其进行格式化。例如:

    这将检索带有帖子信息的对象:

    <?php $postData = get_posts($postParameters);?>
    

    现在从对象中,您收到的日期为:

    [post_date] => 2016-09-20 15:42:55
    

    因此,要将其显示为一个不错的日期,您需要将其格式化为:

    <?php echo '<p>'.get_the_date( 'F, j Y', $postData[0]->ID ).'</p>'; ?>
    

    对于作者,对象返回作者 ID,它是一个数字:

    [post_author] => 1
    

    现在,您可以使用以下代码显示作者显示名称:

    <?php echo '<p>'.the_author_meta( 'display_name', $postData[0]->post_author ).'</p>'; ?>
    

    希望对你有帮助! 享受吧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2017-08-09
      • 2011-03-12
      • 1970-01-01
      • 2014-09-30
      • 2014-05-16
      相关资源
      最近更新 更多