RSS feed blog feed : : del.icio.us : : newsletter : : free magazines : : find a job

Showing Posts When There's No Related Posts

Posted by Ken Cheung on Monday, September 18, 2006 in How-to, WordPress : : 16 comments

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.

If you found this page interesting, bookmark and share it on:
del.icio.us : : digg : : yahoo : : furl : : reddit : : newsvine : : spurl : : blink : : simpy : : blogmarks

Posts of Interest

16 Comments »

2006-09-19 01:47:53

[…] Showing Posts When there's no Related Posts by Ken […]

 
Comment by Matt
2006-09-19 08:14:58

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.

 
Comment by MamaDuck
2006-09-19 08:35:46

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!

 
Comment by MamaDuck
2006-09-19 08:36:30

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

 
Comment by Andrew Flusche
2006-09-19 10:27:41

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!

 
Comment by Jersey Girl
2006-09-19 10:54:24

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

 
2006-09-22 11:03:08

[…] 26. Showing Posts When There's No Related Posts by Ken Cheung […]

 
2006-09-22 17:32:54

[…] Showing Posts When there's no Related Posts by Ken […]

 
2006-09-22 23:02:36

[…] Showing Posts When there’s no Related Posts by Ken […]

 
 
2006-09-23 07:02:58

[…] Showing Posts When there’s no Related Posts by Ken […]

 
2006-09-25 05:47:14

[…] Showing Posts When there’s no Related Posts by Ken […]

 
2006-09-26 09:06:06

[…] Showing Posts When there’s no Related Posts by Ken […]

 
2006-09-26 09:15:12

[…] Showing Posts When there’s no Related Posts by Ken […]

 
2006-09-30 21:59:07

[…] Showing Posts When there’s no Related Posts by Ken […]

 
2008-02-26 03:17:06

Create Sales With Email Newsletter Marketing

Email is one of the popular methods of modern communications. Sending relevant emails to various people who requested your information is the most effective way of establishing a relationship with web surfers and later turns them into your customers. T…

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.

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: