Share/Bookmark

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.

This entry was posted in How-to, WordPress. Bookmark the permalink.

17 Responses to Showing Posts When There's No Related Posts

  1. Pingback: 63 ‘How to’ Posts to Add Spice to Your Day - Group Writing Project Day 1

  2. Matt says:

    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.

  3. MamaDuck says:

    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!

  4. MamaDuck says:

    Ah, forgot to say our list is up as well if you'd like to check it out!!

  5. 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!

  6. Jersey Girl says:

    I didn't realize you could do this..interesting.

  7. Pingback: Working at Home on the Internet

  8. Pingback: Kristonia Ink! » Full List of ProBlogger How To Contest Submissions

  9. Pingback: Change your thoughts » Blog Archive » How to posts - loads of them

  10. Pingback: Ask blushgirl - Romance Advice, Tips and Dating Site Reviews » Blog Archive » Day 1 of the Problogger How-To Articles Submissions

  11. Pingback: Mark Choon » The Biggest, really humongous, ‘How To’ list ever assembled in 4 days!

  12. Pingback: Chewing Pencils: Helping you make money from drawing cartoons!

  13. Pingback: CJCM and IT » Blog Archive » Problogger’s Group Writing Project

  14. Pingback: Cell Phone Know How » 343 How To Posts

  15. Pingback: The Scott English Show - » More How To’s then you Could Poke a Stick At

  16. Pingback: Edit As New Email Newsletters

  17. uwiuw says:

    yes, i think i will try to use your hack, bro. hopefully, it's turn out it work :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

iZachy Newsletter

Don't have time to visit iZachy everyday? Then sign up for my free newsletter. I'll send you an email when I have something to share with you. Your email address will be kept confidential and I will not share, sell, or rent it to anyone. You can unsubscribe at any time by clicking a link in the email.

Enter your email address to sign up for my newsletter:
  

Or you can also sign up for our blog feed.