Automatically generate and display related content whenever you create a new post can keep visitors stay longer on your site. They will get more useful information with list of content similar to the current page they’re viewing. Plus, you also have chance to show how best your website/blog is. It’s really a very good idea, isn’t it?
In this tutorial, I’ll show you 2 ways to do that:
1. Do it yourself
On this method, you need to make some changes on your single.php file in your WordPress Theme Folder. The main idea is will get list of posts have same tags with the current post. Of course, you may custom the algorithm to get the most relevance that suit with your expectation such as they are must on the same category, etc…
Place example code below in the bottom of your post content in your single.php file:
<?php $posttags = get_the_tags(); if ($posttags) { $tags_arr = array(); foreach($posttags as $tag) { $tags_arr[] = $tag->slug; } $args = array( 'orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 10, 'caller_get_posts' => 1, 'tag_slug__in' => $tags_arr ); query_posts($args); if (have_posts()) : ?> <h3>Related content you should read</h3> <div> <ul> <?php while (have_posts()) : the_post(); ?> <li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> <?php endwhile ?> </ul> </div> <?php endif; wp_reset_query(); } ?> |
The code above will show top 10 posts have the same tag with the current page. Let’s see a preview of a article on my site: http://4rapiddev.com/problem-issue-error/dymanic-crm-error-401-unauthorized-when-create-or-retrieve-account-and-contact/
If you just want to display related posts within the same category with the current post, follow code below:
<?php $posttags = get_the_tags(); $categories = get_the_category(); if ($posttags) { $tags_arr = array(); foreach($posttags as $tag) { $tags_arr[] = $tag->slug; } if ($post_categories) foreach($categories as $category) $category_ids = $category->term_id . ","; $args = array( 'orderby' => 'date', 'order' => 'desc', 'posts_per_page' => 10, 'caller_get_posts' => 1, 'tag_slug__in' => $tags_arr, 'cat' => $category_ids ); query_posts($args); if (have_posts()) : ?> <h3>Related content you should read</h3> <div> <ul> <?php while (have_posts()) : the_post(); ?> <li><a title="<?php the_title();?>" href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li> <?php endwhile ?> </ul> </div> <?php endif; wp_reset_query(); } ?> |
2. Use a WordPress Plugin
There are some plugins on both WordPress.org and the Internet but I just suggest 2 plugins I like best.
But my favorite one is the Yet Another Related Posts Plugin and it’s being used on the site because some of features below:
- Easy to install
- Don’t need to make any changes
- The related posts are related
- The algorithm is good
- Full customizable with lot of options
I captured some screens for you can reference:
Yet Another Related Posts Plugin Preview
Yet Another Related Posts Plugin The Pool
Yet Another Related Posts Plugin Relatedness Options
Yet Another Related Posts Plugin Display Options