YARPP : How to use custom templates to show related posts

YARPP using custom templates

Yet Another Related Posts Plugin or YARPP is a very popular WordPress related posts plugin. It supports different display options, the most interesting among them is support for custom templates. YARPP comes with built-in templates from which we can choose, but we can add our own templates as well. This gives us the ability to really customize the look for the YARPP related posts. This article will show how to activate custom templates, how to edit existing or create new ones and finally it will show some useful code snippets that can be used in them.

Here are the steps needed to create our own custom template for related posts:

Step 1 – Activate Custom Templates

First, we need to activate the custom templates in the YARPP plugin. We go to a plugin's settings page in Dashboard > Settings > YARPP. Under Display Options, we have three choices: List, Thumbnail and Custom, which is grayed out. Hovering over the Custom choice will display a message window saying: "This option is disabled because no YARPP templates were found in your theme" as shown below:

YARPP plugin settings - display options with custom option disabled

Click image to enlarge

Click on “Copy Templates” button. This will cause the built-in custom templates in the YARPP plugin to be copied to your current theme folder. After that, the Custom button under Display Option should be enabled. Clicking on it will give you an option with a list of built-in templates to choose from:

YARPP plugin settings - display custom options

Click image to enlarge

Currently, the YARPP version 4.3.1 offers the following custom templates:

Template name Filename Description
Simple yarpp-template-example.php Simple list of related posts. If none are found, "No related posts" message is shown.
List yarpp-template-list.php Returns a comma-separated list.
Multilingual yarpp-template-multilingual.php For use with WPML and Polylang plugins. qTranslate-X plugin can simply use any other template.
Yet Another Photoblog yarpp-template-photoblog.php For use with Yet Another Photoblog plugin.
Random yarpp-template-random.php Same as Simple template, except if no related posts are found, a random post is shown.
Thumbnails yarpp-template-thumbnail.php A list of related posts with post thumbnails.

If none of the above suits your need, then you can either edit the existing template or create a new one.

Step 2 – Add or Edit the custom template file

All of the mentioned custom template files in the above table are located in the active theme folder. They all start with the yarpp-template- prefix, so to make a new template, we would use that prefix and give it a name (for example yarpp-template-myawesomelist.php).

The templates work as a regular WordPress template. We set the template name, author name, and description inside the comment header at the beginning of the template, followed by code containing a WordPress loop that goes through the list of related posts.

Instead of creating a new template, you can of course simply edit one of the existing templates.

Note: If you want to reset the templates and get "Copy templates" option back in the plugin settings, delete all the plugin template files with the “yarpp-template-“ prefix in your theme folder.

Step 3 – Add the PHP code inside the template

Let's examine the code inside the simplest built-in template that renders a list (yarpp-template-example.php):

<h3>Related Posts</h3>
<?php if (have_posts()):?>
<ol>
	<?php while (have_posts()) : the_post(); ?>
	<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><!-- (<?php the_score(); ?>)--></li>
	<?php endwhile; ?>
</ol>
<?php else: ?>
<p>No related posts.</p>
<?php endif; ?>

Line by Line code explanation

  • Line 2:

    <?php if (have_posts()):?>
    

    Here the code checks if there are posts in the loop.

  • Line 4:

    <?php while (have_posts()) : the_post(); ?>
    

    This is where the loop of the posts starts.

  • Line 5:

    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><!-- (<?php the_score(); ?>)--></li>

    This code outputs related post.

  • Line 9:

    <p>No related posts.</p>
    

    Here we add the code when no related posts are found. We can have a simple HTML message as above, or something more complex. For example, in built-in Random template, the code returns a random post.

Some customization examples of YARPP custom templates

Let's examine some useful code, we might want to add to the related posts:

  • Add a post thumbnail to the related posts
    Check YARPP built-in custom template file yarpp-template-thumbnail.php on how to use thumbnail posts. Here is the relevant code in the loop:

    <?php if (has_post_thumbnail()):?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a></li>
    <?php endif; ?>

    You also need to make sure that your theme support post thumbnails by having add_theme_support( 'post-thumbnails' ); code inside the functions.php file.

    For more information check add_theme_support Codex page about about this function.

  • Use custom thumbnail size for related posts
    To use a custom size of the thumbnail, we first need to register a new image size with add_image_size() function inside a functions.php. For example:

    add_image_size( 'widgetthumb', 60, 57, true );
    

    Then in YARPP template code loop, we simply call the_post_thumbnail with the name of previous registered size as the parameter:

    the_post_thumbnail('widgetthumb');
    
  • Add comment information to the related posts
    To add the number of comments and a link to them, we use this code inside the loop:

    <a href="<?php comments_link(); ?>" rel="nofollow"><?php comments_number();?></a>
  • Do not show related posts with specific categories
    Sometimes you might want to make sure that posts with specific categories are not shown in the related posts. We can achieve this by doing something like the code below. Notice that the relevant code is placed just after the loop.

    <?php while (have_posts()) : the_post(); ?>
    <?php
      $categories =get_the_category();
      $skipRelatedPost=false;
      foreach($categories as $category) {
        if ($category->name=="Blogging")
        $skipRelatedPost=true;
      }
      if ($skipRelatedPost)
        continue;
    ?>
    

Conclusion

YARPP, also known as Yet Another Related Posts Plugin is a popular WordPress-related posts plugin. It has support for custom templates, so we can present the related posts in our own way. This article listed the built-in ones and showed how to edit or add new custom templates, examined the code inside one of the template, and finally showed code examples for features that we can add to our own templates.

If you found this article useful, take a moment and drop a comment or share it on social networks.

""

2 Comments

  1. Jean
    June 29, 2018
    • admin
      June 29, 2018

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.