Magazine

How to Display Related Posts in WordPress Without Plugin

Posted on the 12 April 2023 by Jitendra Vaswani @JitendraBlogger

Owning a blog or a website is not a great thing, but to manage the consistency of the blog or a website that’s a hard task. We all (blogger) familiar about the word – “Bounce Rate”, and this word has great importance in our blogging life. 

Bounce rate essentially represents the percentage of initial visitors to a site who jumps away to a different site, rather than continue on to other pages within the same site. If you have a high bounce rate that means your website isn’t impressive enough to explore it more.

There are many reasons why visitors leave your website without exploring it further such as “Broken navigation links”,  “Irrelevant content”, “Popups” and much more. And this problem will be not solved ASAP then the no. of visitors will decrease.

Our revenue totally depends upon the visitors (exceptions are always there), so to decrease the bounce rate of our blog/website and to increase no. of visitors and revenue you can Display Related Posts in WordPress.

Read: How to Install WordPress Using Fantastico

There are many plugins out which helps you to “Display Related Posts in WordPress“, but every time it’s not necessary to use the plugin. Plugin consumes space in your WordPress database.

It also increases the loading time of your website. That’s why today in this article I will show you “How to Display Related Posts in WordPress without Plugin”.

Display Related Posts in WordPress without Plugin

<?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 = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a>

<?php
endwhile;
}
wp_reset_query();
}
?>

Paste the above code in single.php where you want to Display Related Posts in WordPress. This is the easiest way to Display Related Posts in WordPress without Plugin.

If you face any problem in finding the single.php file or in placing the code then you can surely contact us via comments below.


Back to Featured Articles on Logo Paperblog