Showing Posts When There's No Related Posts
Posted by Ken Cheung on Monday, September 18, 2006 in How-to, WordPress : : 16 commentsLike 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:
[…] Showing Posts When there's no Related Posts by Ken […]
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.
[…] 26. Showing Posts When There's No Related Posts by Ken Cheung […]
[…] Showing Posts When there's no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
[…] Showing Posts When there’s no Related Posts by Ken […]
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…