I have a recent posts widget on my wordpress website's right sidebar that is displaying recent posts from multiple post types. I want this widget to also show the post type or category of the post. How can I achieve this?
Use this within the loop get_the_category($id)
To do this manually (i.e by creating your own function), add this in a function and call it when you need it
<ul>
<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php get_the_category(the_ID())->name; // caetgory name ?></li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
You can Code For that recent post in sidebar.php
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args ); ?>
<ul>
<?php foreach( $recent_posts as $recent ){ ?>
<li>
<a href="<?php echo get_permalink( $recent["ID"] ); ?>">
<img src="<?php echo get_the_post_thumbnail_url( $recent["ID"] ); ?>">
<div class="recent_post_meta">
<span>
<?php $category_detail = get_the_category( $recent["ID"] );//$post->ID
foreach( $category_detail as $cd ){
echo $cd->cat_name;
} ?>
</span>
<div class="post_side_title">
<?php echo get_the_title( $recent["ID"] ); ?>
</div>
</div>
</a>
</li>
<?php } ?>
</ul>