Skip to main content

Display Related Posts in WordPress Without Plugins

With WordPress plugins, you can do almost everything to add more feature you want, including display related posts in your blog. But using plugins all the time is not really good for your website. In this article, we will show you why not to use plugin for all features and how to display related posts in WordPress without plugins.

Why should you display related posts?

Let’s say 100 people come to your website to read a blog post. After completing reading the post, they have nothing else to do and quit. It’s definitely a waste. It is not easy to get them on site so don’t let them go that simple. Show them more interesting things, some additional information besides your post. Now it’s time for related posts showing up. By displaying related posts, you can keep them stay longer on your website, increase number of pageviews and reduce the bounce rate.

There are 2 ways to show related posts: using plugins or customize yourself. Using plugins is actually more convenient and easy. You just need to search for a good plugin and install then set up. But why you should consider again?

Disadvantage of using related post plugins?

As mentioned above, using plugins is not always good since you have to reply on a third party so you will not have 100% control over the functions. Sometimes you might find bugs or outdated code that cause security issues.

Besides, using too many plugins may slower your website and it will take your developers time to jump in to fix the errors. Instead, if possible, consider using built in code to have full control and safety of your site.

How to display related posts without plugins?

Copy and paste the code below in file single.php where you want to display related posts

<?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();
}
?>

Source: MichaelH

How about related posts with thumbnail?

Related Posts by Tags

Each of your posts may be have number of tags with multiple keywords. This algorithm would result in a list of posts with one or more similar tags with current post. You can paste this code anywhere you like in your single.php but it is recommended to be placed right above the comments in the main loop.

<?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(); ?>

Related Posts by Category

This algorithm would result in a list of related posts within the same category as the current post. Similarly, you can paste this code anywhere you like in your single.php.

<?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(); ?>

Feel free to ask us any question or discuss more with us in comment. If you find this article useful, please share it to your friends with these social button below.

Related News

How to Add Infinite Scroll to your WordPress Blog

If you want to reduce number of actions that your readers have to take to read...

How to schedule post in your WordPress blog

WordPress allows users to schedule post in their blog. This feature can be quite new...

How to add favicon in WordPress blog

What is a favicon? A favicon (short for "favorites icon") is the small icon that...

Leave a reply