I have a wordpress page where I want am showing the latest 3 blog posts with a function. It's the following, and inside the div "blog-post", I'd like to display in which blog category each of the posts is. I've tried the function "get_the_category()" but it does not work for me, all the other 3 functions work just fine! Any tips?
Tried the following function, but it does only show 1 blog post category, not all (when a post has 2 categories selected, it only shows 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 >
<?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();
?>
CodePudding user response:
Instead of using get_the_category try using the_category.
To get comma separated list
<?php echo the_category(', '); ?>