Related Posts Widget for Blogger

This widget or hack call it how you like is often requested by fellow bloggers.

Here is the tutorial how to install related posts based on labels underneath your blog posts.

First of all you must know that it won`˙t work if your labels use any reserved url characters.

Go to templates edit html and click on expand widget templates and copy the whole code inside a plain text editor like notepad. You might consider backing up your template if something runs wrong.

Now paste the following in the page header :


<script type="text/javascript">
//<![CDATA[
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 < entry.link.length; k++) {
if (entry.link[k].rel == 'alternate') {
relatedUrls[relatedTitlesNum] = entry.link[k].href;
relatedTitlesNum++;
break;
}
}
}
}
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;
document.write('<ul>');
while (i < relatedTitles.length && i < 20) {
document.write('<li><a href="' + relatedUrls[r] + '">' + relatedTitles[r] + '</a></li>');
if (r < relatedTitles.length - 1) {
r++;
} else {
r = 0;
}
i++;
}
document.write('</ul>');
}
//]]>
</script>

Scroll down to the blog1 widget, and find the following:

<b:if cond='data:post.labels'>
<data:postLabelsLabel/>
<b:loop values='data:post.labels' var='label'>
<a expr:href='data:label.url' rel='tag'><data:label.name/></a><b:if cond='data:label.isLast != "true"'>,</b:if>
</b:loop>
</b:if>

and change it to :

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

Save the code back to your template html and go to page elements and add a new HTML/Javascript widget underneath the main blog posts widget with this code :

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

Now go back to Template -> Edit HTML, check the check box to expand the template code, and find the HTML/Javascript widget you just added. It'll look something like the following. Add the lines in bold:

<b:widget id='HTML13' locked='false' title='Related Posts' type='HTML'>
<b:includable id='main'>
<b:if cond='data:blog.pageType == "item"'>
<!-- only display title if it's non-empty -->
<b:if cond='data:title != ""'>
<h2 class='title'><data:title/></h2>
</b:if>
<div class='widget-content'>
<data:content/>
</div>

<b:include name='quickedit'/>
</b:if>
</b:includable>
</b:widget>

There's a few numbers you can tweak to show more or less related posts. In the first code box there is a part that says i < results="10.">

n

11 comments:

Cool Tool said...

Thank you very much.I have just installed on my travel blog , so it is extremely useful for blogs like mine!

E.Khan-Edwardian said...

You are Welcome "Cool Tool".

David said...

Thanks so much for this. All other scripts I had seen would list the related posts under their label headers. I was too lazy to write my own script, so yours is perfect! ;)

David Webb: Photographer and Computer Geek

maximillian_x said...

This is awesome. Thanks again for this - it makes Blogger just that much better!

Cool Tool said...

And the best of all = it's working on all templates.Easy to install and it attracts more visitors!

Manpreet Singh said...

Aweosme Mate

BlogMeTheMoney said...

Thanks for this widget/hack. Instructions were simple to use. Again thanks! Very Appreciated

Using this on Blog Me The Money

SYED MUBALLIGH said...

I got this error while saving HTML

"Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The element type "b:skin" must be terminated by the matching end-tag "".

My Fun Galaxy said...

Wow! At last I managed to do it, I have been looking this "widget" for so long and seek for help in Blogger Discussion and everyone said "No Way" unless we use Wordpress.

Now I'd proved that you are right, they all wrong!

Anyway, the same post also appear in the "related posts" listing. But better than nothing :)

Thanks so much! Feel free to visit my blog for this achievement.

By the way, do you know any "widget" to get "Most Popular Posts"? Thanks.

My Fun Galaxy said...

Oops! you can visit my blog here:

http://myfungalaxy.blogspot.com/

but the "Related Posts" only appear on the article page, not the main page, I don't know why!

Ginkgo100 said...

I have two blogs, which is convenient because I can test your widget v. the one by Hoctro/Jackbook that's also out there. I like the results of yours much better — it looks like what WordPress does, which is what I want. The only problem is that it shows up below my Adsense and my comments (I use Intense Debate). Is this because I have to use a "Page Elements" widget that goes below the Blog Posts element? I'd like this widget to appear in the bottom of the post body like Hoctro/Jackbook's does. Is there a way to do this?

You can see how your widget appears on my first blog, Leave the lights on, compared to the Hoctro/Jackbook widget on my second blog, The Road to Black.