WordPress Related Posts Shortcode

Age of Article Warning:
This article was originally published 66 months ago. The information, tips and techniques explained may outdated. Examples shown on this page may no longer work. Please consider this when viewing the below content.

So you’ve managed to get people to read your post, that’s fantastic. Traffic to your blog is the first step in promoting your blog or business online. But what about when they reach the end of the article? What happens next?

Well this is an ideal location to provide links to other pages on your site or even a “call to action”. Just don’t leave them hanging!

Also adding recent related posts to the end of your WordPress blog post, is great for internal linking, which is good for SEO.

Yes there are plenty of plugins out there that can add “Related Posts” to the end of your blog post, but it’s always a good idea to try to minimise the number of plugins when you can. Remember that the more plugins, the slower your blog will load. And a slow loading blog is a big NO-NO.

So here’s some code that will allow you to add a “Related Posts Shortcode” to your blog posts.

WordPress Related Posts Shortcode

[php]
<?php
/*related posts by tag shortcode */
function dtwd_related_posts_shortcode($atts){
extract(shortcode_atts(array(
‘count’ => ‘3’,
‘title’ => ‘More useful tips’,
), $atts));
global $post;
$current_cat = get_the_category($post->ID);
$current_cat = $current_cat[0]->cat_ID;
$this_cat = ”;
$tag_ids = array();
$tags = get_the_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
} else {
$this_cat = $current_cat;
}
$args = array(
‘post_type’ => get_post_type(),
‘numberposts’ => $count,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘tag__in’ => $tag_ids,
‘cat’ => $this_cat,
‘exclude’ => $post->ID
);
$dtwd_related_posts = get_posts($args);
if ( empty($dtwd_related_posts) ) {
$args[‘tag__in’] = ”;
$args[‘cat’] = $current_cat;
$dtwd_related_posts = get_posts($args);
}
if ( empty($dtwd_related_posts) ) {
return;
}
$post_list = ”;
foreach($dtwd_related_posts as $dtwd_related) {
$post_list .= ‘<li><a href="’ . get_permalink($dtwd_related->ID) . ‘">’ . $dtwd_related->post_title . ‘</a></li>’;
}
return sprintf(‘
<div class="dtwd_related-posts">
<h4>%s</h4>
<ul>%s</ul>
</div> <!– .dtwd_related-posts –>
‘, $title, $post_list );
}
add_shortcode(‘dtwd_related_posts_sc’, ‘dtwd_related_posts_shortcode’);
?>
[/php]

Basically this php code defines a function called “dtwd_related_posts_shortcode” by accessing the current post’s data using “global $post”, then it identifies the current category and tags, grabs the related posts’ links and outputs them onto the page when the function is called.

If the current post doesn’t have tags, or is the only post with that tag, then the function searches for the recent posts from the current category.

Finally you can use this code by simply inserting the shortcode [dtwd_related_posts_sc] onto your post page.

Note that the defaults in the code are count=3 for number of related posts and title=”More useful tips” for the heading for the related posts. You can change this by inserting some attributes into the shortcode like this:
[dtwd_related_posts_sc title=”insert a new title in here” count=”insert a new number in here”]

Feel free to use this shortcode on your blog and amend as required. As you can see below, I am using it on this blog.

Filed under: WordPressTagged with: , , ,

1 Comment

  1. This a great snippet. Can I use it to show just one single link and related post in the middle of my current post? regards!


Comments are closed for this article!