Fix Yet Another Related Posts Plugin (YARPP) Widget and Add it to the Sidebar
YARPP is an essential WordPress plugin because it improves the navigation of your blog and therefore it adds more search engine optimization weight to your pages.
Most blogs list links to related posts at the bottom of each post page. Another great place to list related links is in the sidebar. YARPP does come with a sidebar widget. But as you can see in the plugin’s forum, many people are having problems with it. Also, it doesn’t allow you to separate the style and template from the main related posts links. Even the author of the plugin has disowned the built-in widget!
What I’ll show you here is how you can create your own YARPP sidebar widget and give it a different style and template from the main related posts links.
PHP Code for the Widget
Here’s the entire PHP code for this widget.
add_action('widgets_init', create_function('', 'register_widget("Related_Posts_Widget");'));
class Related_Posts_Widget extends WP_Widget
{
function Related_Posts_Widget() {
parent::WP_Widget(false, $name = 'Related Posts Widget');
}
function widget($args, $instance)
{
extract($args);
extract($instance);
echo $before_widget;
echo related_posts(array('template_file'=>'yarpp-template-widget.php'));
echo $after_widget;
}
function update($new_instance, $old_instance)
{
return $new_instance;
}
}
Copy and paste the code above in functions.php in your theme’s folder. You can now go to “Appearance” -> “Widgets” and you’ll see a new widget called “Related Posts Widget”. Simply drag it to your sidebar! But wait. Before you do that, you’ll have to create a template file for the widget.
Widget Template File
The template file is mostly made up of the HTML that the plugin will use to display your list of related posts.
For the widget code above to work, you’ll have to create a new template file and name it: yarpp-template-widget.php. The file should exist with the rest of YARPP template files, which is in the main folder of your theme.
The content of my yarpp-template-widget.php look like this:
<?php /* Example template Author: mitcho (Michael Yoshitaka Erlewine) */ ?> <?php if ($related_query->have_posts()):?> <h2 class="widgettitle">Other Interesting Posts</h2> <ul> <?php while ($related_query->have_posts()) : $related_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><!-- (<?php the_score(); ?>)--></li> <?php endwhile; ?> </ul> <?php endif; ?>
You’ll need to know some HTML and PHP to modify the template.
But with this file in place, you can now simply go to the Widgets panel in WordPress admin area, and you’ll see “Related Posts Widget”, which you can add to any sidebar!
I hope you found this tutorial helpful. If you have any questions, comments or suggestions, post them below


MK, this looks great. Can I incorporate these changes into YARPP itself, with credit to you of course?
I’d be honored
Hi – this is great, I’ve fixed up my widget now. Do you have an idea about a) how to add a number of related items option and b) whether this does any processing that might use the_content function. I have a custom the_content function and it seems to be manipulating the output of this widget as well.
Chris.
Ignore that – it was outputting YARPP twice (once through widget and once through options). And I had a type in the template name….
Thanks again for posting this.
Hi Chris,
I think it would be useful to be able to specify the number of related posts in the sidebar and having the ability to set a different number for the sidebar widget from the post footer. But from looking at the code, it seems that the number of posts shown is universal.
MK
Thanks MK. I’ve included code based on yours (but changed a fair deal) into a new beta of YARPP:
http://downloads.wordpress.org/plugin/yet-another-related-posts-plugin.3.1.4b3.zip
Thanks for the update, Mitcho. I also sent you an email.