Sunday, November 18, 2007

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 < 20 and the 3rd code box has a part that says max-results=10. You can play around with these numbers if you want. After playing around a little these numbers seem to give a good trade-off between the speed of page-load and the number of related posts displayed.

PurpleMoggy


13 comments:

155cafe said...

thank your for good tips

SVdM said...

Doesn't work for me.. Can you have a look at my blog? It's at http://sandervdm.blogspot.com. Thanks!

Hayden Tennent said...

Works a charm! Hopefully the code gets improved so it does not show the current post as being a related post.

Tech skill said...

great man it worked beautifully.nice work.

CrazyKinux said...

Works beautifully! I've been looking for this for quite sometime now!

CK

Net hunter said...

hey i dont know how to do this pls say that where to use it like before this tag and after the end of this one somewhat like that hope you will do help me

Timethy Jones said...

These are step by step instructions I can install this hack for you if you like for a small fee

Supporter said...

thank you very very much is was successful with me in my blog :
http://no4depression.blogspot.com/
thanks again

Nirose said...

Nice article i am going to use in on my blog

KMc said...

Searchles just released a contextual Related Posts widget last week that's really easy to grab for Blogger. It analyzes the content of your posts for better relevancy so you don't even have to tag. I don't think I can post links here but google "searchles discovery" and you should be able to find it.

RSG said...

after so many trials with other hacks finally yours made it work!!! Thank you very much!!! However, is there a way I can locate the Related Posts before the Comments? And also not to include the current post?

RSG said...

In addition, how can I change the color of the Related Posts title? Pls. take a look at my blog http://architectureoverload.blogspot.com

Pratik said...

hey in the last code you didnt end the b:if tag

Post a Comment


Pimped for blogger by BT | Designed by - Jai