Monday, April 03, 2006

How to create "Latest Comment" per post?

本站另一個方便的function,便是每篇文章末段顯示該篇的最近留言者(Latest Comment)。這個function的製作跟之前Side Bar的Recent Comments差不多,都是在template裡面插入兩段程式碼。它的局限跟Recent Comments一樣,它只適用於Blogger.com的blog,blog的Comments的Comments Timestamp Format要設定為「Monday, November 01, 2005 16:35:00 PM」的式樣。

第一段程式碼要放入<head>...</head>部份,跟Recent Comments一樣:


<script type="text/javascript" language="JavaScript1.2">

/*
Blogger Show Latest Comment Per Post
v1.0

Made by Ka Lok
*/

/* global variables */

var commentCount = 0;
var commentIndex = 0;

// date format converter for new date format for display in latest comment per post
//
// this one converts from:
// Monday, November 01, 2005 16:35:00pm
// to:
// 01 Nov 2005
function lcDateConvertWithDay(dt) {
var s = dt.split(' ');
var d = s[2];
var m = s[1];
var y = s[3];
var t = s[4];

var MonthHash = new Array();
MonthHash['January'] = 'Jan';
MonthHash['February'] = 'Feb';
MonthHash['March'] = 'Mar';
MonthHash['April'] = 'Apr';
MonthHash['May'] = 'May';
MonthHash['June'] = 'Jun';
MonthHash['July'] = 'Jul';
MonthHash['August'] = 'Aug';
MonthHash['September']= 'Sep';
MonthHash['October'] = 'Oct';
MonthHash['November'] = 'Nov';
MonthHash['December'] = 'Dec';

// trim off comma
d = d.substring(0, d.length-1);

// return MonthHash[m] + '/' + d + '/' + y + ' ' + t + ':00';
return d + ' ' + MonthHash[m] + ' ' + y;
}

</script>


第二段程式碼要插進真正顯示最近留言者的html碼之中:


<!-- start of code to show latest comment per post -->

<script type="text/javascript" language="JavaScript1.2">
commentCount = <$BlogItemCommentCount$>;
commentIndex = 0;
</script>

<BlogItemComments>
<script type="text/javascript" language="JavaScript1.2">
commentIndex = commentIndex + 1;
if (commentIndex == commentCount) {
document.write('<em>Latest comment by <$BlogCommentAuthor$> on ');
document.write('<a href="<$BlogCommentPermalinkURL$>">');
document.write(lcDateConvertWithDay('<$BlogCommentDateTime$>'));
document.write('</a></em>');
}
</script>
</BlogItemComments>

<!-- end of code to show latest comment per post -->


每個template顯示的格式可能不同,各人的喜好也未必一樣,所以上面的碼要怎樣放,要你自己花點心思尋找和決定,本站是放在這堆顯示該篇文章有多少個留言的程式碼之後:


<a class="comment-link"
href="<$BlogItemCommentCreate$>"<$BlogItemCommentFormOnclick$>>
<$BlogItemCommentCount$> comments</a>

1 Comments:

At Thursday, April 06, 2006 1:09:00 am, Blogger Terry said...

謝謝提供,Thanks a million!
我去修理一下版面先!

 

Post a Comment

<< Home