在 while 函数中获取 Wordpress 类别

Get Wordpress Categories in a while function

我有一个 wordpress 页面,我想在其中显示具有功能的最新 3 个博客 post。 如下所示,在 div“blog-post”中,我想显示每个 post 属于哪个博客类别。我试过函数“get_the_category()”,但它对我不起作用,所有其他 3 个函数都工作得很好!有什么建议吗?

尝试了以下功能,但它只显示 1 个博客 post 类别,而不是全部(当 post 选择了 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(', '); ?>