dongnan1989 2017-04-12 22:54
浏览 33
已采纳

在Wordpress中获取作者和日期

I am using the below code to obtain post information in Wordpress. I have found this code online and have added the two variables "date" and "author". However, these are not currently working as required/expected.

For "date" it is returning the full date/time stamp (i.e 2017-03-08 15:12:39) rather than the nicely formatted date retrieved when using get_the_date(); (i.e 8th March 2017). Can I adjust my markup to format the date?

For "author", nothing at all is being returned?

Thanks in advance.

$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条回答 默认 最新

  • drlq92444 2017-04-13 02:54
    关注

    This is because you are retrieving an object, after you receive the data you need to format it the way you need it. For example:

    This will retrieve the Object with the post information:

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

    Now from the object, you received the date as:

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

    So to show it as a nice date you need to format it like this:

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

    And with the Author, the object return the author ID which is a number:

    [post_author] => 1
    

    Now with the following code you can show the Author display name:

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

    I hope it can help you! Enjoy!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?