Show related posts with wordpress post without plugin

For reducing bounce rate Show related posts with wordpress post without plugin is really great solution. There are many wordpress plugins which will give similar functionality but it will add extra code server hits to your server. It causes slowness.

Show related posts with wordpress post

Just copy paste the following code in your functions.php file.  You will find this file in your wordpress theme folder.

Show related posts with wordpress post

Show related posts with wordpress post

function get_related_posts($post_id) {
$tags = wp_get_post_tags($post_id);
if ($tags) {
echo 'Check some Related Posts<br>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>10,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$the_permalink = get_permalink();
$post_title =  get_the_title();
$related_post = '<a href="'.$the_permalink.'">'.$post_title.'</a><br>';
echo $related_post;
endwhile;
}
}
}

You just need to use this function in your loop using “get_related_posts();” this function.

Add a Comment

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