Flawless website navigation is the key to grab users’ attention. The convenience of switching from one page/post to another will undoubtedly keep your visitors engaged for a longer duration of time and in a way help your site grow to new heights of success.
If you’re running a WordPress powered blogging site then it becomes quintessential to cut down the browsing time because the more time users spend on reading your articles/blogs, the better it would be for your WP blog’s traffic count.
In this blog, I’ve focused on the reasons why using post tags is the best method for displaying related posts. In addition to this, as you read through this post, you’ll find more on showcasing related posts/articles using post tags.
First off, let me share with you some must-know benefits of using post tags method for displaying related posts on a page
1. There’s an opportunity of displaying a wider collection of posts related to the one that’s being viewed currently
As compared to the restricted count of related blog posts that are displayed using the post categories method, opting for the post tags method allows you to showcase a larger count of posts which contain the same tags as the ones defined for the currently displayed post/article.
2. You are free to display the related posts in any way you want
One of the best things about displaying related blog posts/articles using the post tags method is that you can choose a display type as per your specific preference. That means, you can display the posts in the form of a vertical list, as a scrolling list or in the form of small widgets that appear towards the right side of a web page.
And now, the basic step that marks the display of related posts using the post tags method
In order to display all related blog posts using post tags, all you need to do is simply copy paste the below mentioned code in your WordPress site/blog’s single.php file:
<?php $posttags = get_the_tags(); $tags = ''; if ($posttags) { foreach($posttags as $tag) { $tags .= ','.$tag->name; } $taglist = substr($tags, 1); $args = array('tag' => $taglist, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 10, ); $posts = new WP_Query( $args ); echo '<div class="entry-content">'; echo '<h2>Related Post</h2>'; echo '<ul>'; while( $posts->have_posts() ) : $posts->the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; wp_reset_query(); echo '</div>'; } ?>
If you carefully observe the above code, you’ll find the following:
get_the_tags()-
This is basically a WordPress function which fetches all the tags that are stored as an array in the current blog post.
foreach($posttags as $tag) {
$tags .= ','.$tag->name;
}
This is a php loop which interacts with the array of tags stored for the blog post and renders a list of similar tags one by one.
query_posts
This is basically a WordPress function that passes the tags list(derived from running the foreach php loop explained above) and renders the list of blog posts that use the same tags.
Here, I’ve used UI and LI HTML tags for displaying the related blog posts in a vertical serial order as 1, 2, 3,,,and so on. You can opt for using a different pair of HTML tags for adding visual excellence to the representation of related blog posts. Displayed below is a screen-shot for how the related posts will be displayed after implementing the aforementioned step:
Since a majority of codes available over the web can’t be used within the main post loop, the aforementioned code has actually turned extremely helpful for positioning the related posts right after the main post and above the comments section. You can easily place this code anywhere in your single.php file and it will work fabulously. Whether you are opting for a numbered list of related blog posts or are inclined on showcasing the posts within the menu located towards the right side of the web page, this code will allow you to do all this and much more in a remarkable way.
Final Words
The ‘Related Posts’ section within a website is an amazing means of keeping the users engaged for longer durations of time. By paying special heed to the appropriate display of these related blog posts/articles, you can increase readership for your blog in addition to attaining a unique niche in the world of WordPress blogging.