Display Related Posts in WordPress
Related posts are those posts that are in relation to the current post and are most likely to be viewed by the current reader. All those posts that share the same topic are called related posts. For example, the current post is more likely to boost SEO, so all the posts that help boost SEO will be its related posts.
The idea behind displaying the related posts
How will WordPress be instructed that some other post is related to the current post? Well, this is pretty easy. How? The related posts will have the same category, tag, or some custom Meta tag. So related posts can be fetched based on category, tag or some other meta tags.
How to display related posts in WordPress?
In WordPress, which is the most used content management system (CMS), there is always a plug-in for implementing your idea. There are also pros and cons to using plug-ins in WordPress. The advantage is that the task is accomplished without any coding. The disadvantage is that it slows the website because extra CSS and JavaScripts related to the plug-in are also loaded with normal page load. Another disadvantage is security holes. Many websites have been hacked so far due to security vulnerabilities in the plug-ins.
Display related posts in WordPress with a plug-in
You can use the following plugins to display related posts in WordPress.
- Related posts for WordPress
- WordPress related posts
- Yet Another Related Post Plug-in
- Contextual related posts
- Yuzo related posts
- Inline Related posts
Click here if you don’t know how to install a plugin in WordPress.
Display related posts without plug-in
To display the related posts without a plugin, paste the following piece of code into single.php function in a suitable place (after the comments usually).
Source: MichaelH
<?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();
}
?>