I have somehow managed to create this code to display Related Posts by category on single post page.
<div class="relatedposts">
<h3>You might also like</h3>
<?php
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=> 4, // Number of related posts that will be shown.
'caller_get_posts'=>1);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>" title ="<?php the_title(); ?>" ><?php the_post_thumbnail(); ?><br /><?php the_title(); ?></a>
</div>
<?php } // ending while loop
}
$post = $orig_post;
wp_reset_query();
?>
</div>
Problem is, this code doesn’t displays related posts of the same category as on single post page. Even includes the same post displayed on single post page and also shows some recent posts.
You can test yourself. Please suggest how can I resolve this or am I missing some thing in this code?
Thanks