How to Display Related Posts in WordPress without a Plugin

How to Display Related Posts in WordPress without a Plugin

When a reader comes to your site, he/she may be searching for a particular topic. Sometimes, you can’t cover everything about a topic in one post.

There may need some additional information. So, when you show a related post on the topic, audience read that one too. As a result, you get the audience trust.

That means, this is essential for any bloggers. But most of the new bloggers find this task difficult. Especially, when they want to avoid a plugin.

A plugin can make this task easy, But, codes also work better. In this post, you will learn how to show a related post on your WordPress blog without a plugin.

Related Posts

Why Shouldn’t You Use a Plugin?

Maybe you are wondering why you shouldn’t use a plugin for a related post. As it is easy to use, you may want to add a plugin. This plugin is easy to use, but it has some drawback.

When you are using a plugin, you need to depend on the third party. If the plugin has an issue, you need to contact the developer.

Again, if the plugin doesn’t update regularly, it may cause an error. Most of the free plugins come with a poor coding standard.

Also, this is a slow process. Sometimes it may return an error for some posts. Moreover, there are fewer options in a plugin. So, it is better to use a code on your WordPress blog instead of a plugin.

Related Posts By Tags

WordPress has a by default option for posts. This is known as tags. When you are posting something on your blog, you can use variety of tags.

Now, if you are posting another article similar to this, you can display it for readers. For showing the posts by tags, you can use the following code.

<?php $orig_post = $post;

global $post;

$tags = wp_get_post_tags($post->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),

‘posts_per_page’=>5, // Number of related posts that will be shown.

‘caller_get_posts’=>1

);

$my_query = new wp_query( $args );

if( $my_query->have_posts() ) {

echo ‘<div id=”relatedposts”><h3>Related Posts</h3><ul>’;

while( $my_query->have_posts() ) {

$my_query->the_post(); ?>

<li><div class=”relatedthumb”><a href=”<? the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_post_thumbnail(); ?></a></div>

<div class=”relatedcontent”>

<h3><a href=”<? the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>

<?php the_time(‘M j, Y’) ?>

</div>

</li>

<? }

echo ‘</ul></div>’;

}

}

$post = $orig_post;

wp_reset_query(); ?>

This code will look at the current post ID and the tags of the post. It uses the wp_query function to match the tags for showing as related post. However, you can customize the code for different styles.

Related Posts by Category

If you want to show related posts by category, you need to use another code. When you will post a new article in the same category, the code will show it as related posts. For this method you need to use the following codes.

<?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),

‘posts_per_page’=> 2, // Number of related posts that will be shown.

‘caller_get_posts’=>1

);

$my_query = new wp_query( $args );

if( $my_query->have_posts() ) {

echo ‘<div id=”related_posts”><h3>Related Posts</h3><ul>’;

while( $my_query->have_posts() ) {

$my_query->the_post();?>

<li><div class=”relatedthumb”><a href=”<? the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_post_thumbnail(); ?></a></div>

<div class=”relatedcontent”>

<h3><a href=”<? the_permalink()?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>

<?php the_time(‘M j, Y’) ?>

</div>

</li>

<?

}

echo ‘</ul></div>’;

}

}

$post = $orig_post;

wp_reset_query(); ?>

Code Credit: Source.

Benefits of Related Post On WordPress

There are several benefits of showing a related post in a new blog specially. The first thing is, it will reduce the bounce rate of your site. How?

When you will show a related post, there is a good chance that reader will go for that one too. So, the reader will stay for more time on your site.

This is a good signal for any site. Giant search engine Google gives importance on several factors for ranking. A helpful page layout is also one of them.

Related post is also helpful to get the audience attraction. It helps people to find related content on your site.

In addition, bounce rate is also a major factor to hit rankings. High bounce rate might be a reason of low ranking of your site.

So, by decreasing the bounce rate, you can rank in a better position. It will help you to gain rankings and helping your visitors as well.

What you Learnt from this Post are-
  1. You can use the code for showing related post based on tags.
  2. For showing associated posts according to the category, you need to use a different code.
  3. You can customize the code based on your necessity.

I hope that found helpful information in this post. If you liked this post, feel free to share it with others.

One Response

  1. Neha

Write a Comment