List posts from the same category on single sidebar

Seems people actually read this blog; so thought I’d pass this along – How to list posts that are in the same category when you are on a single.php page…

<?php if ( is_category() ) {
global $wp_query;
$current_cat_id = $wp_query->get_queried_object_id(); ?>
<div id="category-posts-<?php echo $current_cat_id; ?>" class="widget widget_recent_entries">
<div class="widget-title"><h3><?php single_cat_title('Recently in '); ?>:</h3></div>
<div class="widget-content">
<ul>
<?php global $post; $cat_posts = get_posts('numberposts=10&category='.$current_cat_id);
foreach($cat_posts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php } ?>

This blog’s turning out to be a quick reference wiki for code – wow…

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>