Home >> Latest Articles >> How to Display Related Posts in WordPress Without a Plugin

How to Display Related Posts in WordPress Without a Plugin

Wordpress related posts: There are two ways of showing wordpress related posts in your website.
  1. Displaying wordpress related posts with a plugin
  2. Displaying wordpress related posts without a plugin
Display wordpress related posts without a plugin:
  1. Display Related Post by Tags
  2. Display Related Post by Categories

You need to open your single.php files in WordPress editor and paste these lines of code where you want to display related posts on your website.

In Latest Version of Wordpress, you need to open your content-single.php files in WordPress editor and paste these lines of code where you want to display related posts on your website.

Display wordpress related posts by Tags
ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) 
$tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_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);
if( $my_query->have_posts() ) {
echo '';
}
}
wp_reset_query();
?>
Display wordpress related posts by Categories
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),
'posts_per_page'=> 5, // Number of related posts that will be displayed.
'caller_get_posts'=>1,
'orderby'=>'rand' // Randomize the posts 
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo ''; 
} }  
wp_reset_query(); 
?>
Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles