Fix Yet Another Related Posts Plugin (YARPP) Widget and Add it to the Sidebar

2010 January 19
by MK

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 :)

Do any of these posts interest you, too?

  1. Clean Your WordPress Sidebar to Improve Navigation and SEO!
  2. Give Posts Their Own Sidebar in WordPress
  3. Sensible Use of “Tags” in a WordPress Blog
  4. Turn Keywords into Links with Word 2 Cash
7 Responses leave one →
  1. January 19, 2010

    MK, this looks great. Can I incorporate these changes into YARPP itself, with credit to you of course?

    • MK permalink*
      January 19, 2010

      I’d be honored :)

  2. February 2, 2010

    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.

  3. February 2, 2010

    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.

    • MK permalink*
      February 2, 2010

      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

  4. February 24, 2010

    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

    • MK permalink*
      February 25, 2010

      Thanks for the update, Mitcho. I also sent you an email.

Do you have a question, suggestion, or comment? Tell me!

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS