2009年4月9日星期四

给Blogger添加相关文章功能

本Hack是根据标签来判断文章相关性的,注意标签中不能包含像?/之类的字符,对文章的标签数量也没有限制。在咖啡鱼的教材中,是将相关文章放在 "页面元素"中,需要添加一个HTML/Javascript模块。本例有点改动,将相关文章放在文章后面,自己比较喜欢这种形式。

1、把下面代码放在<head></head>中。其中的"i < 12"是控制文章数量的,建议数值不要太大,以免影响浏览速递。

<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 < 12) {
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>

2、找到下面黑色的代码,在</b:if>与</b:loop>之间,插入如下红色的代码。注意:"max-results=12"是控制相关文章的显示数值。
<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=12"' type='text/javascript'/>
</b:if>

</b:loop>
</b:if>


3、找到自己想要显示相关文章的位置,将下面红色代码放进去,保存就OK那了。本例就放在评论前,如下面黑色代码。
<b:if cond='data:blog.pageType == "item"'>
<script type="text/javascript">
removeRelatedDuplicates(); printRelatedLabels(); </script> </b:if>

<div id='comments'>
<a name='comments'/>
<h4>
<b:if cond='data: post.numComments == 1'>
1 条评论:
<b:else/>
<data:post.numComments/> 条评论:
</b:if>
</h4>
<dl id='comments-block'>
<b:loop values='data: post.comments' var='comment'>
<dt class='comment-author' expr:id='"comment-" + data:comment.id'>
<a expr:name='"comment-" + data: comment.id'/>
<b:if cond='data:comment.authorUrl'>
<a expr:href='data:comment.authorUrl' rel='nofollow'><data:comment.author />

4、完成,看看效果吧!

8 条评论:

匿名 说...

[url=http://levitranowdirect.com/#nfivl]buy levitra online[/url] - levitra 20 mg , http://levitranowdirect.com/#pjnbs cheap generic levitra

匿名 说...

[url=http://viagranowdirect.com/#vrrzp]buy viagra[/url] - cheap generic viagra , http://viagranowdirect.com/#enhql cheap viagra online

匿名 说...

[url=http://viagraboutiqueone.com/#hcohi]viagra online[/url] - viagra online , http://viagraboutiqueone.com/#bkusr buy viagra

匿名 说...

[url=http://buyonlineaccutanenow.com/#qmquo]accutane 5 mg[/url] - accutane without prescription , http://buyonlineaccutanenow.com/#yptrv accutane 10 mg

匿名 说...

[url=http://orderviagradirectlyonline.com/#dimwe]viagra without prescription[/url] - purchase viagra , http://orderviagradirectlyonline.com/#igiec viagra 25 mg

匿名 说...

[url=http://buyreliablezithromaxonline.com/#jmqvn]buy cheap zithromax[/url] - cheap zithromax online , http://buyreliablezithromaxonline.com/#wccvw zithromax 500 mg

匿名 说...

Tutorials cialis generic cialis,cialis cialis, generic viagra generic cialis,buy viagra cialis
http://genericcialispricegnf.com#cialis online http://cialisonlinepillsgkd.com#cialis pills http://genericviagraonlinejvrg.com#generic viagra http://buyviagraonlinertvr.com#buy viagra

匿名 说...

You would be amazed at what number of everyone is reducing their auto insurance costs by $500 or higher. Medical payment coverage also covers the insured in addition to their family within the event of being struck by way of a vehicle while walking, jogging, and running, etc. You will also get understanding of different forms of coverage and many types of sorts of specifics of different companies once you research in order to find Texas auto insurance quotes with the assistance of engines like google on internet. xSZdudg, [url=http://www.igetcheapcarinsurance.com/alberta-auto-insurance-quotes-online/]alberta auto insurance quotes online[/url], poCOMhw, [url=http://www.igetcheapcarinsurance.com/ez-auto-insurance-arkadelphia-ar/]ez auto insurance arkadelphia ar[/url], xAjnFSi, [url=http://www.igetcheapcarinsurance.com/motor-insurance-gender-divide-under-threat/]motor insurance gender divide under threat[/url], ZLNmPog,