在while函数中获取Wordpress分类



我有一个wordpress页面,我想显示最新的3篇博客文章与一个功能。如下所示,在div "blog-post"中,我想显示每个帖子属于哪个博客类别。我尝试了函数"get_the_category()"但它不适合我,所有其他3个功能工作得很好!任何建议吗?

尝试以下函数,但它只显示一个博客文章类别,而不是全部(当一个帖子有2个类别选择时,它只显示1)

<?php echo wp_get_post_terms(get_the_ID(), 'category')[0]->name; ?>
<?php 
$the_query = new WP_Query( 'posts_per_page=3' ); ?>
<?php 
while ($the_query -> have_posts()) : $the_query -> the_post(); 
?>

<div class="blog-post">
<?php echo get_the_post_thumbnail( $the_query->ID, array( 400, 0) ); ?>
<?php echo get_avatar( get_the_author_meta( 'ID') , 150); ?>
<?php echo get_the_author_meta('display_name', $author_id); ?>
</div>
<?php 
endwhile;
wp_reset_postdata();
?>

与其使用get_the_category不如使用the_category。

获取逗号分隔的列表

<?php echo the_category(', '); ?>

最新更新