2008-02-09

【Blogger】最新文章與最新回應產生器

在凍仁的 New x 10 的 block 就列出了最新10筆文章的標題,網路上也有部落客把文章上限設為999直接列出所有文章.. 也可以設定單 label 裡最新的文章... 等等。

<div id="newpostsjonnyubuntu">
    <h2>Loading...</h2>
</div>

<script type="text/javascript">
function compareentry(a,b){
    order= Date.parse(a.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/,
  '$1/$2/$3 $4 GMT')) - Date.parse(b.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/,
  '$1/$2/$3 $4 GMT'));
    return 0-order;

}

function handlePostsjonnyubuntu(json) {

    var title = '<h2>New Posts</h2>';
 if (title == '<h2></h2>')
   title = '';
 var temp = title + '<ul id="Feed001_feedItemListDisplay">';
    var postshow=5;
 var titlelen = '';
 var layout = "%title% #%Y%-%M%-%D%";
    var sortentry=json.feed.entry.sort(compareentry);
    for (var i=0, post; post = sortentry[i]; i++) {
        if(i>=postshow) break;
        var title=post.title.$t;
  if (titlelen != "" && title.length > titlelen)
    title = title.substr(0, titlelen) + "...";
        var link=post.link[2].href;
  var title_link = '<a href="'+link+'">'+ title +'</a>';
        var authorname=post.author[0].name.$t;
        var timestamp=post.published.$t.substr(0,10);
    var y = timestamp.substr(0, 4);
    var m = timestamp.substr(5, 2);
    var d = timestamp.substr(8, 2);
    
  var layout_replace = layout.replace("%title%",title_link).replace("%Y%",y).replace("%M%",m).replace("%D%",d).replace("%authorname%",authorname);
  
        temp += '<li><span class="item-title">'+layout_replace+'</span></li>';
      }
temp+="</ul>";
document.getElementById("newpostsjonnyubuntu").innerHTML = temp;
}
</script>

<script src="http://jonnyubuntu.blogspot.com/feeds/posts/summary/?alt=json-in-script&callback=handlePostsjonnyubuntu" type="text/javascript"></script>

資料來源:
布丁布丁吃?- Blogger專用最新文章與最新回應產生