Show Related Posts on Blogger (Increase User Engagement & Page Views)

Adding a section in Blogger that displays related posts can increase user engagement, page views, and can reduce bounce rate. Learn how to add related posts to Blogger's post page using JavaScript and JSON feeds.

Related Posts on Blogger

One of the strategies that websites implement to keep users engaged is to show related posts, products, services, or other content related to the current page's topic. This post demonstrates how to display related posts with thumbnails on Blogger without using any widget.

 

How to Show Related Posts with Thumbnails on Blogger?

It is very simple and easy to show related posts on Blogger in a few steps with basic knowledge of JavaScript and JSON object structure. The implementation requires the following steps:

  • Loop through all the labels of the current post in view.
  • Call the Blogger API to get posts with the same labels.
  • Prepare HTML of the related post box and exclude the post in view.
  • Finally, render the related posts on the post page where you want to display them.
 

Step 1: Retrieve Posts by Labels in Blogger

The first step is to place an element container where the posts are supposed to display. Then make a call to Blogger's Data API to retrieve other posts that have the same labels as the current post. A step-by-step explanation of the code is as follows:

  • Add an element container with a specific ID or class attribute to select it later. We are using related-posts as the class attribute.
  • Start a loop for each label of the current post and make an API call to retrieve posts against the current label in the loop. The endpoint URL contains the alt=json-in-script query parameter to return the response as JSON, and the callback=prepare_related_posts_list parameter as the callback function that will be triggered after the request has finished. The max-results=6 to set the maximum number of posts to be returned from the endpoint.
  • Declare two variables, current_post_link and max_related_posts, after the loop. The current_post_link will be used to exclude the current post from the related posts list, so we do not show the same post in view in the related posts section. Whereas max_related_posts is the number of posts we want to show.
<b:if cond='data:blog.pageType == "item"'>
<div class='related-posts'/>
<b:loop values='data:post.labels' var='label'>
<script defer='defer' expr:src='"/feeds/posts/default/-/" + data:label.name + "?alt=json-in-script&callback=prepare_related_posts_list&max-results=6"' type='text/javascript'/>
</b:loop>
<script type='text/javascript'>
let current_post_link = '<data:post.url/>';
let max_related_posts = 4;
</script>
</b:if>
 

Step 2: Prepare and Render Related Posts Section

The next step is to prepare a list of related posts in our callback function and call another function to render the posts after the window's load event. The callback function uses another function to prepare the URL of a single post and implements the following code logic:

  • Declare related_posts and related_filled variables before the function. The related_posts variable will hold the posts we need to render, and related_filled will be used to avoid duplicate posts in the related posts list.
  • Loop through each post and prepare a JSON of a single post containing the "title", "image", and "URL" values.
  • Call the get_single_post_link() function, passing the post object as a parameter.
  • Fetch the HTML content of the post and loop through all images in the content. Set the first image found in the content in the JSON of a single post.
  • Add the post to the related_posts and related_filled arrays if it does not exist in the related_posts array.
  • Finally, add the window load event listener and render the related posts using the render_related_posts() function.
<script type='text/javascript'>
//<![CDATA[
let related_posts = [];
let related_filled = [];

function prepare_related_posts_list(json) {
let posts = json.feed.entry;

posts.forEach(function(p, v) {

//- Prepare the link of related post
let p_link = get_single_post_link(p);
let related_post = {
title: p.title.$t,
link: p_link,
image: {
el: "",
src: ""
}
};

//- Get the HTML content of the post that will be used to retrieve the post image
let content_html = new DOMParser().parseFromString(p.content.$t, 'text/html');
let image = content_html.querySelector("img");

//- If image is found, set the image src
if (image instanceof HTMLImageElement) {
related_post.image.el = image;
related_post.image.src = image.src;
}

//- If post link is not equal to current link and not already filled then add to list
if (p_link != current_post_link && !related_filled.includes(p.title.$t)) {
related_posts.push(related_post);
related_filled.push(p.title.$t);
}
});

//- Finally print the related posts after the document has loaded
window.addEventListener("load", function () {
render_related_posts();
});
}

//- Function to prepare the post link in loop
function get_single_post_link(p) {
let link = "javascript:void(0)";
if ("link" in p) {
for (let i = 0; i < p.link.length; i++) {
if (p.link[i].type == "text/html" && p.link[i].rel == "alternate") {
link = p.link[i].href;
break;
}
}
}

return link;
}

//- Function to render related posts after document load
function render_related_posts() {
if (related_posts.length) {
let html = "";

html += `<h3>Related Posts:</h3>`;
html += `<div class="row">`;

//- If related posts list has reached the number of posts we need, break the loop
for (let i = 0; i < related_posts.length; i++) {
if (i >= max_related_posts)
break;

html += `
<div class="col-6">
<div class="related-post">
<div class="related-post-image">
<a href="${related_posts[i].link}">
${related_posts[i].image.el.outerHTML}
</a>
</div>
<div class="related-post-title">
<a href="${related_posts[i].link}">${related_posts[i].title}</a>
</div>
</div>
</div>`;
}

html += `</div>`;
document.querySelector(".related-posts").innerHTML = html;
}
}
//]]>
</script>

Displaying related posts on any website is a great way to increase page views, improve internal linking, and reduce bounce rate. We demonstrated how to create a Blogger related posts with thumbnails section that renders posts associated with the labels of the current post. The working example of this feature can be seen at the end of this page.