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>
<ul>
<?php
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) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</ul>

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 *

2 Comments

  1. Thanx!!!!!!!!! 🙂