List of related posts is a convenient addition for you as the blog’s owner and administrator and the blog readers. On one hand the readers are able to flow between posts collecting lots of useful information without spending much time or effort on searching. One the other hand you as the owner are happy that no page on your blog goes under the readers radar. Besides the list of related posts is a sort of linkage which means a degree of SEO your blog gets as a bonus.
In other words ‘related posts’ function is a must-have for every blog. One thing which not everyone may know is how to actually implement it in the blog. Behold and cheer up, this isn’t really a drudgery. There’re two ways of adding related posts function: one is easy, using a plugin, another is a bit more labor intensive, done without the plugin. Being guided by our advices you can choose one for yourself.
The easy way amounts to using plugins. One such plugin is Simple Tags. Although its main purpose is tags processing, it has an indispensible feature bonus of related posts processing, namely a capability of such posts lists generation. Another, a significantly ‘lighter’ and therefore more attractive plugin, is called
WordPress Related Posts, which works exclusivley with related posts.
Download WordPress Related Posts plugin from WordPress homepage, unpack it, upload via FTP and activate following the standard procedure. After the activation you will need to insert the following line of code into the template: < ?php wp_related_posts(); ?> Then we go to the settings, where we specify the title, a phrase which will be displayed in a case when no related posts are found and the number of entries to display. Filters, communication with RSS can also be configured.
If you don’t find plugin to be the best solution, for example if you strive to minimize the strain your blog is under, related posts feature can be implemented manually. Thus the labor intensive way amounts to using a shortcode inserting the following code at any suitable place within the file functions.php between tags <? and ?>:
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
'limit' => '5',
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = '<ul>';
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);
// Do the query
$q = "SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";
$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
}
} else {
$retval .= '
<li>No related posts found</li>
';
}
$retval .= '</ul>
';
return $retval;
}
return;
}
add_shortcode('related_posts', 'related_posts_shortcode');
}