WordPress – List Recent Posts From A Single Category on Posts Within Same Category

WORDPRESS 3.0+

I wanted to display the recent post titles from a specific category, to visitors who landed on a post within that specific category (and i didn’t want to use a plugin).

List posts from a specific category ONLY on pages within specified category:

<?php if (in_category('32')) { ?>
<div class="moduletable">
<h3>Latest Snippets</h3>
<ol>
<?php
global $post;
$myposts = get_posts('numberposts=5&order=DESC&orderby=post_date&category=32');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
<?php endforeach; ?>
</ol>
</div>
<?php }else { ?>
<?php } ?>

NOTE – I’ve bolded

  • 5 (number of posts to display) and
  • 32 – the category ID

Theses are the items I used in the example :)

If you enjoyed this post, please share :)

Written by Shaun Anderson