在 Wordpress 中获取 post 作者和日期
Getting post author and date in Wordpress
我正在使用以下代码在 Wordpress 中获取 post 信息。我在网上找到这段代码并添加了两个变量 "date" 和 "author"。但是,这些目前不作为 required/expected。
对于 "date",它返回完整的 date/time 戳记(即 2017-03-08 15:12:39),而不是使用 get_the_date 时检索到的格式正确的日期( ); (即 2017 年 3 月 8 日)。我可以调整我的标记来格式化日期吗?
对于 "author",什么都没有返回?
提前致谢。
$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);?>
现在从对象中,您收到的日期为:
[post_date] => 2016-09-20 15:42:55
因此,要将其显示为一个不错的日期,您需要将其格式化为:
<?php echo '<p>'.get_the_date( 'F, j Y', $postData[0]->ID ).'</p>'; ?>
对于作者,对象 return 作者 ID,它是一个数字:
[post_author] => 1
现在,您可以使用以下代码显示作者显示名称:
<?php echo '<p>'.the_author_meta( 'display_name', $postData[0]->post_author ).'</p>'; ?>
希望对您有所帮助!
享受吧!
我正在使用以下代码在 Wordpress 中获取 post 信息。我在网上找到这段代码并添加了两个变量 "date" 和 "author"。但是,这些目前不作为 required/expected。
对于 "date",它返回完整的 date/time 戳记(即 2017-03-08 15:12:39),而不是使用 get_the_date 时检索到的格式正确的日期( ); (即 2017 年 3 月 8 日)。我可以调整我的标记来格式化日期吗?
对于 "author",什么都没有返回?
提前致谢。
$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);?>
现在从对象中,您收到的日期为:
[post_date] => 2016-09-20 15:42:55
因此,要将其显示为一个不错的日期,您需要将其格式化为:
<?php echo '<p>'.get_the_date( 'F, j Y', $postData[0]->ID ).'</p>'; ?>
对于作者,对象 return 作者 ID,它是一个数字:
[post_author] => 1
现在,您可以使用以下代码显示作者显示名称:
<?php echo '<p>'.the_author_meta( 'display_name', $postData[0]->post_author ).'</p>'; ?>
希望对您有所帮助! 享受吧!