Build your own Related Posts widget for Blogger

I have visited many blog sites and most of them have "Related Posts" or "You may also like - " section for visitors who like to discover more related articles or stories. I am one of those visitors who likes to find out more interesting posts which are related to the original article I have read. Indeed, having related posts section will increase your exposures of your site. That is no doubt about it.

Unfortunately, I didn't have this feature on my site since I started to customise it. Of course I then started to do some research about building a "Related Posts" widget for Blogger. Obviously, there are lots of articles talking about "adding" related posts widget to your Blogger site but not many of them have detailed explanations. Well, fortunately some of them guided me to create this jQuery plugin. 

So first of all, what does related posts mean? Are they related to the title of the original article? Or are they related to the category of the original post? Or are they related to the author's posts? Well, I would answer yes to all of them. You may have other supportive answers too.  

In this plugin, I find related posts based on the current article's "labels" via the Blogger's RSS feed. So you need to label your article in order to make this plugin work. Since the RSS feed provides all information I needed, I just need to extract and parse the data to what I need for "related posts". So what do I need from the RSS feed?

  • The post id or labels of current article
  • The labels of each article
  • The URL link of each article
  • The title of each article
  • The thumbnail link of each article

Luckily Google provides a comprehensive documentation for its data APIs for Blogger developers so if you want to dig more information out, just pay a visit to here.

Anyway, the concept is to use the labels of current article to find related posts and the post id or article link as identifier to exclude current article from the collection of related posts. When the labels are retrieved, then we start to find and collect posts whose labels contain the original article's labels. Finally, we output these collected posts to the site. Here is the source code of this plugin.
   var me = this,
   var settings = $.extend(true,{
    url: '',
    title:'Related Posts: ',
    itemClass: 'related_item',
       wrapperClass: 'related_items_wapper',
    titleClass: 'title',
    maxPosts: 6
    settings.labels = settings.labels.trim().split(',');
   return init();
   function init(){
        url: settings.url + '/feeds/posts/default?alt=json&max-results=500',
        type: 'get',
        dataType: "jsonp",
        success: function(data){
         // get the blog feed id
         blogId =$t;
         if(settings.postId !== ''){
          // get the current post id
          postId = blogId + '.post-' + settings.postId;
         // get related posts
         var posts = findRelatedPosts(data.feed.entry);
            // add section title
            var wrapper = $('<div/>',{'class':settings.wrapperClass});
         // construct the output
            for (var i = 0; i < posts.length; i++){
             var entry = posts[i];
             var href = getLink(entry);
             // the link
             var link = $('<a/>',{'href':href});
             // the thumbnail
             var thumb = findThumbnail(entry,entry.title.$t);
             // the post title
             var pTitle = $('<div/>',{'class':settings.titleClass});
             // the item block
             var div = $('<div/>',{'class':settings.itemClass});
             // add thumbnail
                // wrap the link
             // add post title
                      // add the item block
       { window.location = href; })
            // clear the layout
           me.append('<div style="clear:both;"></div>');
   // gets the entry link
   function getLink(entry){
    var href = '#';
    $.each(, function(i, v){
      href = v.href;
      return false;
    return href;
   // find related entries
   function findRelatedPosts(entries){
    var posts = [];
    var cats = settings.labels;
    for (var i = 0; i < entries.length; i++){
    // shuffle array
     posts.sort(function() { return 0.5 - Math.random() });
    return posts.slice(0,settings.maxPosts);
   function findThumbnail(entry, title){
      var div = $('<div/>').html(entry.content.$t);
      var imgs = div.find('img');
      if(imgs.length > 0){     
       return $('<img/>',{'src':imgs[0].src, 'alt': imgs[0].alt, 'title': title, 'width':settings.thumbWidth,'height':settings.thumbHeight});
     return $('<img/>',{'src'$thumbnail.url,'title': title,'alt':title,'width'$thumbnail.width,'height'$thumbnail.height});
   function matchCurrentPost(entry){
    var match = false;
     var id =$t
     match = (id == postId);    
     var href = location.href.replace(location.hash,'');
     href = href.split('?')[0].toLowerCase();
     $.each(, function(i,v){
      if(v.rel == 'alternate'){
       if(v.href.toLowerCase() == href){
        match = true;
        return false;
    return match;
   // find any matched category
   function matchCategory(entry, tags){
     for(var i = 0; i < entry.category.length; i++){
      var tag = entry.category[i].term;
      if($.inArray(tag,tags) >= 0){
       return true;
    return false;
You may notice that I use the url /feeds/posts/default instead of /feeds/posts/summary. If I use summary, then I cannot extract the original images for thumbnails. Also if the images of the article were not uploaded via Blogger then you don't have the thumbnail link from the feed. Therefore the most secure way is use the original image link from the article itself.

So how can I use this widget?

There are 4 key factors you need to do:
  1. Make sure you have jQuery library loaded
  2. Make sure you have the plugin placed after the jQuery library link
  3. Place the div container to the place you want to show (usually we place it at the end of the article) 
  4. Call the plugin when the document is ready

Let's get started and we only need 3 steps to make it work

1. Place the code to the section you want to show the related posts

Below is the code you need to put to the place you want to show on your page
<b:if cond='data:blog.pageType == &quot;item&quot;'>
 <!-- related posts -->
 <div id='related_posts'></div>
 <script>var labels = &#39;<b:loop values='data:post.labels' var='label'><><b:if cond='data:label.isLast != &quot;true&quot;'>,</b:if></b:loop>&#39;;</script>                  
First you need to open your HTML template editor and search for

<div class='post-footer'></div>

if you cannot find above, then we need to find below text

<b:includable id='post' var='post'>

when you found above part, you need to scroll down until you see


when you see it, place the sample code above it.

2. Place the CSS code to your skin

Search the text below in your HTML template editor


then place the code below above the ]]><b:skin>
 border-top: 1px solid #d0d0d0;
 border-bottom: 1px solid #d0d0d0;         

#related_posts h2 {
 background: #211f1c url( fixed;
 font-weight: normal;
 letter-spacing: -0.5px;
 font-size: 18px;
 line-height: 33px;
 font-family:"Trebuchet MS", Helvetica, sans-serif;

 padding: 10px;

 overflow: hidden;
 text-align: center;
 border-radius: 10px;
 border: 1px solid #ececec;
 margin-right: 10px;
 -moz-box-shadow: 0 8px 4px -4px #000;
 -webkit-box-shadow: 0 8px 4px -4px #000;
 box-shadow: 0 8px 4px -4px #000;
   -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
    -o-transition: all .5s ease;
   -ms-transition: all .5s ease;
    transition: all .5s ease;

 -moz-box-shadow: 0 0 4px 4px #ddd;
 -webkit-box-shadow: 0 0 4px 4px #ddd;
 box-shadow: 0 0 4px 4px #ddd;
 border: 1px solid #ddd;
 background: #eee;
 cursor: pointer;

.related_item:hover img, .related_item:hover .title{
 opacity: .7;

.related_item img{
 border-top-left-radius: 10px;
 border-top-right-radius: 10px;

.related_item .title{
 font-size: 90%;
 text-align: center;
 background: #555;
 padding:10px 5px;
 max-width: 90px;

.related_item .title a{
 text-decoration: none;

3. Execute the plugin

Search the text below in your HTML template editor


then place the code below above

<!-- comment out below if you have jQuery library loaded already -->
<script src='//' />
<script src=''/>
  // related posts
  if($('#related_posts').length > 0){
    $('#related_posts').relatedPosts({labels:labels,title:'You may also like -'});
Now, you should be able to see your related post on your page. You can tweak the query to search articles related to the author or give it a date range. If you don't know how to do it, just leave a comment and I will help you. If you have other suggestions or ideas, please also leave your comment.

Related Posts

In case you missed it

A short update from me
A short update from me
It has been a while since I post a blog page on Blogger which was created back in 2013. So I took an opportunity to...
Showcase the blog site elements
Showcase the blog site elements
The standard paragraphs Welcome to this demo page! Here, you’ll get an exclusive preview of our cutting-edge platform designed to revolutionise your digital experience. Our...
Issue with
Issue with "Don't track your own pageviews"
Do you use your own domain name with Blogger? Do you place “Blogger’s Stats Widget” on your page? Or do you regularly check up the...