Showing Posts When There's No Related Posts

Like many of you, I use the Related Posts plugin. I've even hacked the plugin a couple of times to deal with unrelated pages showing up as related and to exclude certain categories from the results. Today, I'm going to show you how to hack the plugin to deal with "no related posts" results.

The hack is actually very simple. I should have done this long ago. If it wasn't for Problogger's group writing project, this hack might still be hidden in my head. In my hack, I changed the plugin to show two particular posts when the plugin cannot find any related posts. You don't have to display two posts, you can go with just one post or as many posts as you want. Normally, the plugin displays "no related posts" but I hate it when it does that. Here's how to change the plugin:

  • Log into the admin area of your WordPress blog.
  • On the right is a list of your plugin files. Click on Related Posts.
  • In the Editing related-posts.php text window, scroll down until you find the line below:

$before_title.'No related posts'.$after_title;

Note: before you make any changes, you should make a back up of the file (select all, copy, paste it to a text file, and save it on your computer).

The line is about halfway down. If you are using a text editor, edit the file named "related-posts.php" in your plugin directory. The line you want to edit should be line #112. Replace 'No related posts' with the link and title of your choosing. For instance, I replaced the "no related posts" message with this post (word wrap not intentional):

echo $before_title.'<a href="http://izachy.com/2006/06/12/i-did-not-get-your-newsletter/">Are Your Subscribers Getting Your Newsletters?</a>'.$after_title;

If you like, you can include the rel and title attributes:

echo $before_title.'<a href="http://izachy.com/2006/06/12/i-did-not-get-your-newsletter/" rel="bookmark" title="Are Your Subscribers Getting Your Newsletters?">Are Your Subscribers Getting Your Newsletters?</a>'.$after_title;

If you want to include another post, just add the second post after the first one (word wrap not intentional):

echo $before_title.'<a href="http://izachy.com/2006/06/12/i-did-not-get-your-newsletter/" rel="bookmark" title="Are Your Subscribers Getting Your Newsletters?">Are Your Subscribers Getting Your Newsletters?</a>'.$after_title;
echo $before_title.'<a href="http://izachy.com/2006/05/15/show-more-adsense-ads-on-long-posts/" rel="bookmark" title="Show More Adsense Ads on Long Posts">Show More Adsense Ads on Long Posts</a>'.$after_title;

The second "echo $before_title" starts on a new line. When picking which posts to use, you should pick ones that have related posts. Otherwise, the post will be related to itself in the results.

18 thoughts on “Showing Posts When There's No Related Posts

  1. WOW! I finally found someone who has figured it out! This has bothered me for quite some time! Thanks for the How To!!! I am going to try this soon! Thanks for contributing to the Group Writing Project. My How To is up if you want to check it out.

  2. This is awesome! It will be particularly useful for 404 error pages, when there is nothing substantial for the plugin to search for. With this hack, every page (even those that don't exist) will have related posts. Thanks!

Comments are closed.