How to Display Related Posts in WordPress211

How-to-Display-Related-Posts-in-WordPress
Author: InfoBeep - December 14, 2017 | subject to copyright

Displaying related posts adds flavor to your site. It helps you get more viewer attention and attracts viewer to some other articles in your page. In addition, it helps you decrease your bounce rate. Now, if you want to display related post in WordPress this is what you need to do.

Procedure

Simply copy and paste the code below in your single.php where you find its relevant to display related post.

<?php

//for use in the loop, list 5 post titles related to first tag on current post

$tags= wp_get_post_tags($post->ID);

if($tags) {

echo'Related Posts';

$first_tag= $tags[0]->term_id;

$args=array(

'tag__in'=> array($first_tag),

'post__not_in'=> array($post->ID),

'posts_per_page'=>5,

'caller_get_posts'=>1

);

$my_query= newWP_Query($args);

if( $my_query->have_posts() ) {

while($my_query->have_posts()) : $my_query->the_post(); ?>

<a href="<?phpthe_permalink() ?>"rel="bookmark"title="Permanent Link to <?phpthe_title_attribute(); ?>"><?phpthe_title(); ?></a>



<?php

endwhile;

}

wp_reset_query();

}

?>

Source: MichaelH

Note: the above plugin is called Yet Another Related Post Plugin (YARP)