WordPress Recent posts from Current / Same Category

If you want to display recent posts from same category in which current posts resides then following code inside single.php will display recent 5 posts from same category.

Just add following code inside single.php file at the location where you want to display recent posts.

<h3>Recently Post From Same Category</h3>
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
foreach($myposts as $post) :
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
<?php endforeach; ?>
<?php wp_reset_query(); ?>

If you want more number of posts just change numberposts’ => 5 just change 5 to a different number.

If you want to display recent posts excluding current category in which current post resides then try this code on my other post

WordPress Recent Posts excluding current category

Leave a Reply

Your email address will not be published. Required fields are marked *