Related Posts Widget For Blogger with CSS

Related Posts Widget for blogger is what everybody is looking for, In Most of the sites we will find only javascript code and HTML tag for related posts widget. After adding the code to your blog , you can see just a list of related posts without any arrows or style css. So i wanted to embed both in order to make the widget links looks beautiful.

First let’s see the usual javascript and HTML Tag. Step 1 : Add the following Javascript code below ]]></b:skin> , thats between ]]></b:skin> and </head> .

<script type=”‘text/javascript'”>
var relatedTitles = new Array();
var relatedTitlesNum = 0;
var relatedUrls = new Array();
function related_results_labels(json) {
for (var i = 0; i < json.feed.entry.length; i++) {
var entry = json.feed.entry[i];
relatedTitles[relatedTitlesNum] = entry.title.$t;
for (var k = 0; k <; k++) {
if ([k].rel == ‘alternate’) {
relatedUrls[relatedTitlesNum] =[k].href;
function removeRelatedDuplicates() {
var tmp = new Array(0);
var tmp2 = new Array(0);
for(var i = 0; i < relatedUrls.length; i++) {
if(!contains(tmp, relatedUrls[i])) {
tmp.length += 1;
tmp[tmp.length – 1] = relatedUrls[i];
tmp2.length += 1;
tmp2[tmp2.length – 1] = relatedTitles[i];
relatedTitles = tmp2;
relatedUrls = tmp;
function contains(a, e) {
for(var j = 0; j < a.length; j++) if (a[j]==e) return true;
return false;
function printRelatedLabels() {
var r = Math.floor((relatedTitles.length – 1) * Math.random());
var i = 0;
while (i < relatedTitles.length &amp;&amp; i < 20) {
document.write(‘<li><a href=”‘ + relatedUrls[r] + ‘”>’ + relatedTitles[r] + ‘</a></li>’);
if (r < relatedTitles.length – 1) {
} else {
r = 0;

Step 2 : Check expand widgets and search for <b:loop values=’data:post.labels’ var=’label’> , Now copy and paste the following code in blue between <b:loop values=’data:post.labels’ var=’label’> and </b:loop>

<b:loop values=’data:post.labels’ var=’label’>
<a expr:href=’data:label.url’ rel=’tag’><></a><b:if cond=’data:label.isLast != &quot;true&quot;’>,</b:if><b:if cond=’data:blog.pageType == &quot;item&quot;’> <script expr:src=’&quot;/feeds/posts/default/-/&quot; + + &quot;?alt=json-in-script&amp;callback=related_results_labels&amp;max-results=5&quot;’ type=’text/javascript’/> </b:if> </b:loop>

customize the text in green to show as many links you want.

Step 3 : Now paste the following script the code just below <p><data:post.body/></p> .

code :

<script type=’text/javascript’> removeRelatedDuplicates(); printRelatedLabels(); </script>

Thats it , now look at the page , it will look like this

You can see there is no styling , I wanted to make it look beautiful . So i made the following changes . Both step 1 and 2 are same , just in step three make some changes .

<b:if cond=’data:blog.pageType == “item”‘>
&lt;div id=&quot;related-posts&quot;&gt;
<h2>Other Recommended Posts</h2>

<script type=’text/javascript’> removeRelatedDuplicates(); printRelatedLabels(); </script>


now its time to add css part for this div section .

/*– (Related posts widget -below posts) –*/
#related-posts {

#related-posts .widget{

#related-posts .widget h2, #related-posts h2{
font-size: 1.6em;
font-weight: bold;
color: #0000FF;

font-family: Georgia, “Times New Roman”, Times, serif;
margin-bottom: 0.75em;
margin-top: 0em;
padding-top: 0em;


#related-posts a{

#related-posts a:hover{

#related-posts ul{
margin:0 0 0px 0;

#related-posts ul li{
background:transparent url( no-repeat ;
margin-bottom: 13px;

padding-left: 30px;

Now look at the page again ,

You can even customize the link color , arrow style , font-size , etc I hope you liked this related posts widget , see you soon with another widget or hack.

If you like this article , then please help us by bookmarking

18 Responses to “Related Posts Widget For Blogger with CSS”

  1. Your script is exactly what I’ve been looking for… but its not working for me. Just doesn’t list the related posts. I’ve gone through it a few times and something is not working right. Would appreciate any help.

  2. Hi Latino Insurgent !

    send me your template I will upload this hack for you . Or Very soon I will upload a video tutorial for this hack , So wait for some time.

    email :

    Blogger Accessories

  3. Thanx Rias, no need anymore. There was a conflict between this hack and another that I had running. Took me a little while to figure it out but all is well now. I appreciate the offer. Good work by the way.

  4. my script is also can’t work like your demo,
    only can seen a lot of labels
    thanks for your response

  5. Sorry I could not to hack this one was still requesting his guidance???
    i am blog

  6. Hi PenCuri Kode,

    Please send me your template code so that i can add this hack in your template for free.

    email :

    Blogger Accessories

  7. Hi Mohamed,

    I suspect there is something wrong with my template.

    I followed the first three steps of your tutorial but the related posts are not being listed.

    I suspect the problem is with my template because when I insert Adsense adds between posts (via blogger panel), they are accepted but not exibhited as well.

    Any Tips?

    I just found your blog and it is really excellent.


  8. Hi Amilcar ,

    I need more informations to help you . Tell me which template your are using . if you send me your template , it will be very useful to check errors. If the XHTML is not valid then also problems occur.

    Blogger Accessories

  9. Hi Sheik,

    My template was converted from WP theme. This is my particular template:

    Here is where I picked it from:
    (blog and web converted it as well, but this is a different conversion)

    And here is the demo blog:

    I suspect the problem is with my template because the adsense ads between posts are not showed as well.

    I will send the template file to Mohamed Rias email right away.

    Please let me know if you need any other information.

    Thanks a lot and keep up the good work!

  10. Hi Everyone ,

    All those who got error while adding this related posts widget , please see this post. I have explained thoroughly how to add Related Posts Widget on this post.

    Blogger Accessories

  11. Thanks for the hack….

  12. Finally got it working on using method 2. However, the max-results seems not to be working, it shows 12 when it’s set to 5, 9 when it’s set to 3 and 5 when it’s set to 2.

    But I’m happy with it showing 5 so I’ve set it to 2…

    Thanks for this!

  13. Actually those numbers aren’t always the number shown for some reason.

    And why is the actual post itself shown in its own Related Blogs list?

  14. yeah… i can’t include on my template, i have an error message..

    Earn Money Online

  15. Salam Muhammad…i want this widget..i cant make i need your help…i will send my template code to your email…


  16. Hey, I tried this, and it works great, except it’s showing “No related posts found” on some posts. But I know that there are other posts under the same labels of those posts.

    This also happened when I tried RealTrix’s hack, on the exact same posts.

    Why is this? Is there any way to fix it?

  17. Wait, nevermind, I just realized I hadnt removed his hack from my template. Now I have, and it’s not working at all???

  18. rias,what happened to your website? where is the download link to this template? where is all those comments?

Leave a Reply

You must be logged in to post a comment.