Related Posts with Thumbnail for Blogger / Blogspot

Related Posts Widget is a utility that is quite important. By helping visitors see the related article, they will stay on our site longer. This Widget will search for and print out a random articles have the same label.

Related Posts with Thumbnail for Blogger / Blogspot

Adding related posts widget to Blogger / Blogspot

To make it easy to edit, replace or delete if you do not want to use it. I will create it in the b:includable tag. Now find
<b:includable id='post' var='post'>
above it paste the following code.
<b:includable id='related-posts'>
<style type='text/css'>
#related-posts {float:left}
#related-posts ul{margin:0;padding:0;list-style-type:none}
#related-posts ul li{padding:8px 0}
#related-posts img{float:left;border:1px solid #BBB;margin-right:10px;width:72px;height:72px;background:#FFF;padding:3px}
#related-posts h3{margin:0;font-size:16px}
</style>
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<div style='clear:both'/>
<div class='col-sm-6' id='related-posts'>
<script type='text/javascript'>
var ry='<h2>RELATED POSTS</h2>';
rn='<h2>No related post</h2>';
rcomment='Comments';
rdisable='Comments Disabled';
commentYN='yes';
</script>
<script type='text/javascript'>
//<![CDATA[
var dw='';titles=new Array();titlesNum=0;urls=new Array();timeR=new Array();thumb=new Array();commentsNum=new Array();comments=new Array();function related_results_labels(c){for(var b=0;b<c.feed.entry.length;b++){var d=c.feed.entry[b];titles[titlesNum]=d.title.$t;for(var a=0;a<d.link.length;a++){if('thr$total'in d){commentsNum[titlesNum]=d.thr$total.$t+' '+rcomment}else{commentsNum[titlesNum]=rdisable};if(d.link[a].rel=="alternate"){urls[titlesNum]=d.link[a].href;timeR[titlesNum]=d.published.$t;if('media$thumbnail'in d){thumb[titlesNum]=d.media$thumbnail.url}else{thumb[titlesNum]='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXtARBGHl-4orQ4vfU5jPcGitN72J1ZgTjiOAUja3MWDHnRiUCt9rxYlr1Pspu0m6L01o58q13ve_SE9fiIqJvIRiwcr5VKDAxaa_WYqrXiQYMzbARylH5Kj8i8NWVpLkx86LTZD1IbukO/'};titlesNum++;break}}}}function removeRelatedDuplicates(){var b=new Array(0);c=new Array(0);e=new Array(0);f=new Array(0);g=new Array(0);for(var a=0;a<urls.length;a++){if(!contains(b,urls[a])){b.length+=1;b[b.length-1]=urls[a];c.length+=1;c[c.length-1]=titles[a];e.length+=1;e[e.length-1]=timeR[a];f.length+=1;f[f.length-1]=thumb[a];g.length+=1;g[g.length-1]=commentsNum[a]}}urls=b;titles=c;timeR=e;thumb=f;commentsNum=g}function contains(b,d){for(var c=0;c<b.length;c++){if(b[c]==d){return true}}return false}function printRelatedLabels(a){var y=a.indexOf('?m=0');if(y!=-1){a=a.replace(/\?m=0/g,'')}for(var b=0;b<urls.length;b++){if(urls[b]==a){urls.splice(b,1);titles.splice(b,1);timeR.splice(b,1);thumb.splice(b,1);commentsNum.splice(b,1)}}var c=Math.floor((titles.length-1)*Math.random());var b=0;if(titles.length==0){dw+=rn}else{dw+=ry;dw+='<ul>';while(b<titles.length&&b<20&&b<maxresults){if(y!=-1){urls[c]=urls[c]+'?m=0'}if(commentYN=='yes'){comments[c]=' - '+commentsNum[c]}else{comments[c]=''};dw+='<li><img alt="'+titles[c]+'" src="'+thumb[c]+'"/><div><h3><b><a href="'+urls[c]+'" rel="nofollow">'+titles[c]+'</a></b></h3><span>'+timeR[c].substring(5,7)+'/'+timeR[c].substring(8,10)+'/'+timeR[c].substring(0,4)+comments[c]+'</span></div></li><div style="clear:both"></div>';if(c<titles.length-1){c++}else{c=0}b++}dw+='</ul>'};urls.splice(0,urls.length);titles.splice(0,titles.length);document.getElementById('related-posts').innerHTML=dw};
//]]>
</script>
<b:loop values='data:post.labels' var='label'>
<script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=related_results_labels&quot;' type='text/javascript'/>
</b:loop>
<script type='text/javascript'>var maxresults=3;removeRelatedDuplicates();printRelatedLabels(&#39;<data:post.url/>&#39;);</script>
</div>
</b:if>
</b:includable>
You can change
  • ry: Title of related posts
  • rn: Title if there are no related posts
  • rcomment: Comments phrase
  • rdisable: Comments Disabled Phrase
  • commentYN: Show comments Yes or No
  •  maxresults: Number of comments
Now put this code to anywhere you want to show related posts
<b:include name='related-posts'/>
If you want to show it at the end of post find
<div class='post-footer'>
and paste that code below it.

Save your template and you are done.

2 comments

This comment has been removed by the author.

Removing the line b:if cond='data:blog.pageType == "item"', works on the homepage only the first posting How to change it???

We love to hear from you! Leave us a comment.