[英]get the author name in recent post wordpress
i want to display the the name of the author in my recent blog post . 我想在最近的博客文章中显示作者的名字。 i tried this but not working.
我尝试了这个但没有用。 somebody please help me.
有人请帮助我。
$args = array( 'numberposts' => '8' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){?>
<i><?php echo get_the_author(); ?></i>
<i><?php echo $recent['post_author']; ?></i>
}?>
get_the_author();
it doesnt shows any output. 它不显示任何输出。 And
$recent['post_author'];
还有
$recent['post_author'];
it shows output as 1 它显示为1
get_the_author()
won't work outside the post loop therefore isn't appropriate for use with wp_get_recent_posts()
. get_the_author()
在post循环外不起作用,因此不适用于wp_get_recent_posts()
。
Your second version, $recent['post_author']
, is outputting the correct value. 您的第二个版本
$recent['post_author']
输出正确的值。 You're seeing 1
because it's the author's ID rather than their name. 您看到的是
1
因为它是作者的ID,而不是他们的名字。 You need to take the ID and use it to retrieve their name. 您需要获取ID并使用它来检索其名称。
Use the_author_meta()
to output the display name value: 使用
the_author_meta()
输出显示名称值:
<?php the_author_meta( 'display_name', $recent['post_author'] ); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.