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.
Pingback: 63 ‘How to’ Posts to Add Spice to Your Day - Group Writing Project Day 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.
Oh my goodness, you are my hero. I'm going to try this here in a bit, what a great idea and amazing that you figured that out!
Ah, forgot to say our list is up as well if you'd like to check it out!!
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!
I didn't realize you could do this..interesting.
Pingback: Working at Home on the Internet
Pingback: Kristonia Ink! » Full List of ProBlogger How To Contest Submissions
Pingback: Change your thoughts » Blog Archive » How to posts - loads of them
Pingback: Ask blushgirl - Romance Advice, Tips and Dating Site Reviews » Blog Archive » Day 1 of the Problogger How-To Articles Submissions
Pingback: Mark Choon » The Biggest, really humongous, ‘How To’ list ever assembled in 4 days!
Pingback: Chewing Pencils: Helping you make money from drawing cartoons!
Pingback: CJCM and IT » Blog Archive » Problogger’s Group Writing Project
Pingback: Cell Phone Know How » 343 How To Posts
Pingback: The Scott English Show - » More How To’s then you Could Poke a Stick At
Pingback: Edit As New Email Newsletters
yes, i think i will try to use your hack, bro. hopefully, it's turn out it work :)
Pingback: How to posts - loads of them