Related Posts in WordPress Without Plugin

In this article we’ll discuss about related posts in WordPress. How to display them without a plugin and why are Related Posts important. Many people use it, but some of them only use them because others do as well. They don’t know the reason of it, why it helps and other small important things.

Related Posts

Why are Related Posts Important for a Website or Blog

Related Posts as you might expect, display related contact to the specific article you are viewing. And by using related posts to display similar content, the visitors of your website might be interested in another article that has been displayed there. This means they stay more on your website, which reduces the bounce rate. (You may use Google Analytics to check bounce rate).

How to Add Related Posts in WordPress Without Plugin

To be able to add this functionality to your WordPress website, you’ll need to have a bit coding skill. We provide you a walk through about how to do it step by step. But you need to know how to access your themes files. (Default path is: www.example.com/wp-contant/themes/mytheme).

After you’ve found your themes folder, search for functions.php and create a backup of it. Then open it up. This is the file which contains every custom functions used by your theme. The first thing we need to do is to create our function. So scroll down to the bottom of the file and add the following:

function my_related_posts() {
}

This will be your function that will display the related content to each of your posts when someone views it. After this we need to add some arguments. Arguments are used to let the function know what we want to be similar in the related articles and in the current one. To do this, we add a variable called $args. (Function Reference/wp parse args) In this variable we store two things. Firstly how many posts we want to display and secondly what are the conditions for the articles to be display. Add the following inside your function:

$args = array(
        'posts_per_page' => 5,
        'post_in'  => get_the_tag_list()
);

The next step is to create another variable, called $the_query. (Class Reference/WP Query) This variable is used by WordPress to it display the posts. Used the following code to add our arguments to it:

$the_query = new WP_Query( $args );

Now what we need to do is actually display the posts. To do this we use a while loop which looks like the following:

echo '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
        ?> 
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
        <?php
endwhile;
echo '</ul>';

The code above will display each related post as an anchor tag (link). The fine step is to reset the query. Add the following piece of code to do this:

wp_reset_postdata();

Your final code should look like the following:

function my_related_posts() {
$args = array(
        'posts_per_page' => 5,
        'post_in'  => the_tags()
    );
echo '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
        ?> 
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
        <?php
endwhile;
echo '</ul>';
 wp_reset_postdata();
}

Use the following code to display the posts where you want:

my_related_posts()

If you’re using Genesis Framework used the following snippet:

add_action('genesis_after_content','my_related_posts');

Adding Some Style to the List

The code above will just display a simple list. But that doesn’t look too nice right? So why don’t we add some style to it. Like a small thumbnail or the data when the post was written, or in which category it belongs to. Well, to do this we need to add some extra code into our code.

To display Related Posts before the list, add the following code before the echo ‘<ul>’;

echo '<h2>Related Posts</h2>';

If you want to add thumbnails to it, add the following code before code below:

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

The code:

<?php if ( has_post_thumbnail() ) { ?>
<div class="related_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
<?php } ?>

So your code will look like this:

while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li>
<?php if ( has_post_thumbnail() ) { ?> 
<div class="related_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div> 
<?php } ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
endwhile;

Stylish Display

If you want to display the post as thumbnails with the title below it near each other, like on the image, use the codes below:

Related Posts Thumb

The Function Code:

function ll_related_posts() { 
$args = array(
'posts_per_page' => 5,
'post_in'  => get_the_tag_list(),
); 
$the_query = new WP_Query( $args ); 
echo '<section id="related_posts">'; 
echo '<h2>Related Posts</h2>'; 
while ( $the_query->have_posts() ) : $the_query->the_post();
?> 
<section class="item">
<?php if ( has_post_thumbnail() ) { ?> 
<section class="related_thumb"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'related-post' ); ?></a></section> 
<?php } else { ?>
<section class="related_thumb"><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory')?>/lib/images/thumb.png" /></a></section>
<?php } ?> 
<?php the_title(); ?>
</section>
<?php
endwhile; 
echo '<div class="clear"></div></section>'; 
wp_reset_postdata(); 
}

The CSS

#related_posts {
	margin-top: -10px;
	margin-bottom: 10px;
	padding-bottom: 20px;
	border-bottom: 20px solid #E2E2E2;
}

#related_posts .item {
	width: 132px;
	max-height: 200px;
	overflow: hidden;
	float: left;
	margin: 6px;
	font-weight:bold;
	text-align:center;
}

#related_posts .item img {
	-webkit-box-shadow: 0 0 4px #999;
	box-shadow: 0 0 4px #999;
	border: white solid 1px;
	padding: 4px;
	margin: 0 auto;
	background: #f2f2f2;
}

Conclusion

In this article we learned how to display related posts in WordPress without a plugin and why is it important. Also now we know how to stylize them to be displayed. So, why to use a plugin to display related posts when we can do it without a plugin? If you have any questions, feel free to ask them in the comment section below.


About Andor Nagy

Andor Nagy is a 17 years old student, who is interested in modern technology, mostly web development and web design.

Andor has written 4 awesome articles for us at TechLila.


Comments

  1. Great post, Andor. I didn’t know that it is possible to have reklated posts with thumbnails, which look mich more pleasant for the eyes. Also, we shouldn’t forget about the SEO function of the related posts as they create wonderful interlinking network within your site.
    Marcel Fischer recently posted – Best Sports and Fitness Android AppsMy Profile

  2. I was previously using nrelate related post plugin for my blog but now after reading this article i will definitely try this to my blog..
    Thanks for the article..
    Shashank Johri recently posted – LG Google Nexus 5 Rumored Features, Specs, Price And Release DateMy Profile

  3. When a reader finds an article very interesting, he will look at the related articles for further reading. People who want more information rely on this. Thanks for posting.

  4. Big thanks for this code snippets – I search along time for such a solution without activating for every new feature a special plugin which will cost a lot of ram…

  5. hello admin,
    your post has been very enlightening and full of learning…
    I’ve been greatly motivated by your post….i m a new blogger and would greatly appreciate you advice and important tips regarding blogging
    thank you :)
    akansha recently posted – Students of NIFT protest against Fee HikeMy Profile

  6. Highly informative article. I’ve always been wondering on how to do that. Thanks for writing this article.

  7. Everytime I get interested in an article, I often see the related posts so that I ca find more about it. I’m keen on knowing a lot more stuff that’s why I do it. Related posts are definitely a must.

  8. When I have excess time for reading, I find related posts very helpful. It enables me to know more without searching the net for another related write up. It is indeed very helpful.
    Emilia recently posted – Hiring Professionals for Your Writing NeedsMy Profile

  9. This post is great!!
    Related post option is very necessary as when the user visits the blog the user can also refer to the related post to read something more..This can also help to keep the user on the blog for a long time..
    Vincent Armstead recently posted – Samsung Galaxy S4 Features and SpecificationsMy Profile

  10. i like this but it seem a little bit complicated for beginner to follow
    Juan recently posted – Brave Film ReviewMy Profile

  11. Hey Andor! Thanks for sharing these tips! Really smart job!
    Dragos recently posted – Best Floor Steam Cleaners For 2013My Profile

  12. I use nrelate as well, but I’m curious as to why you would spend time building your own. Would there be any advantage over the plugin? Personally, I try to use as little plugins as possible and relaying on 3rd to pull data for you. What it they start to charge for the service, or what if their server goes down? I prefer to keep as much development of features in house as much as possible. nrelate works well for me. But I’m will to give this a try.

  13. Thanks for this tutorial. I am learning wordpress now a days for my next blog. This is very helpful one….

  14. never seen this before…but i will try it.Thank for this info..
    faris recently posted – How to increase blog traffic with five tipsMy Profile

  15. Rajesh, I am not using Related posts Plugin in my blog. I am also thinking to implement this post to decrease my blog’s bounce rate. It is one of the most important plugin WordPress.
    Thasariya Shakil recently posted – Chhattisgarh Assembly Elections 2013 Predictions Exit Poll Survey ResultsMy Profile

  16. Hi Andor..Really helpful tutorial…I manage a few WordPress sites and Blogger sites…Do you know any hack to get this similar feature in Blogger…Most ‘Related Posts’ widget do not work with blogger…They also do not have any extensions for the same.
    Joe Hart recently posted – Dental ImplantsMy Profile

Comment Policy:

Your words are your own, so be nice and helpful if you can. Please, only use your REAL NAME, not your business name or keywords. Using business name or keywords instead of your real name will lead to the comment being deleted. Anonymous commenting is not allowed either. Limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. You can wrap code in [lang-name][/lang-name] tags.


Tell us what you're thinking...

If you want a picture to show with your comment, then get Gravatar!

CommentLuv badge
This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 5 approved comments. Use your Real Name and then @ your keywords (maximum of 3)