Web Hosting

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 from, but we can add our own templates as well. This gives us 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 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 list of built-in templates to choose from:

YARPP plugin settings - display custom options

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

Line by Line code explanation

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

  • This is where the loop of the posts starts.

  • This code outputs related post.

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

    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:

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

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

  • 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.

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 a 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. Let me know, if you have any other suggestion for modifying the custom template and I might add the necessary code in the list.

Share this page

Add a Comment

Back to Top