Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Wordpress中获取文章作者和日期_Php_Wordpress_Date_Author - Fatal编程技术网

Php 在Wordpress中获取文章作者和日期

Php 在Wordpress中获取文章作者和日期,php,wordpress,date,author,Php,Wordpress,Date,Author,我正在使用下面的代码获取Wordpress中的帖子信息。我在网上找到了这段代码,并添加了两个变量“date”和“author”。但是,这些设备目前未按要求/预期工作 对于“日期”,它返回完整的日期/时间戳(即2017-03-08 15:12:39),而不是使用get_the_date()时检索到的格式良好的日期;(即2017年3月8日)。我可以调整标记以设置日期格式吗 对于“作者”,什么都不返回 提前谢谢 $postData = get_posts($postParameters); $post

我正在使用下面的代码获取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);
}

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

这将检索包含post信息的对象:

<?php $postData = get_posts($postParameters);?>
因此,要将其显示为一个好的日期,您需要将其格式化为以下格式:

<?php echo '<p>'.get_the_date( 'F, j Y', $postData[0]->ID ).'</p>'; ?>
现在,使用以下代码可以显示作者显示名称:

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

我希望它能帮助你! 享受吧

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