Home » Labs » How to show related posts by category in WordPress  

How to show related posts by category in WordPress


Filed under Labs

Below code will display related post from the same category. You need to make changes according to your needs, in this example it will display title and post excerpt. Add the code on your WordPress template.

 
	<?php
	$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 10, 'post__not_in' => array($post->ID) ) );
	if( $related ) foreach( $related as $post ) {
	setup_postdata($post); ?>
		<ul> 
			<li>
				<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
				<?php the_excerpt();?>
			</li>
		</ul>   
	<?php }
	wp_reset_postdata(); 
	?>

Posts you may like: