在Wordpress中获取作者和日期

时间:2017-04-12 22:54:45

标签: php wordpress date author

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

对于“日期”,它返回完整的日期/时间戳(即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);
}

1 个答案:

答案 0 :(得分: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>'; ?>

我希望它可以帮到你! 享受!

相关问题