【Blogger】記事に最終更新日を付ける方法【data:post.lastUpdatedISO8601】

  • 投稿 : 2018-02-20
blogger-last-updated01.png

上手くいくとこんな感じになるかと思います。

サンプルコード

<span class='updated' expr:title='"Post was updated on " + data:post.lastUpdatedISO8601' id='updatedateinfo' itemprop='dateModified'>
    <data:post.lastupdatedISO8601/>
</span>
<script>
function zepad(str){
    return ('0' + str).slice(-2);
}
</script>
<script>
// 投稿日
var published=new Date("<data:post.timestampISO8601/>");
// 最終更新日  
var updated =new Date("<data:post.lastUpdatedISO8601/>");
</script>
<script>
// <![CDATA[
  var updated_str ="最終更新日:"+ updated.getFullYear()+"年"+(updated.getMonth()+1)+"月"+updated.getDate() +"日";
  if(updated.getFullYear() == published.getFullYear() && updated.getMonth() == published.getMonth() && updated.getDate() == published.getDate()){
    //投稿日と同じときは表示しない
  }else{
    document.getElementById("updatedateinfo").innerHTML = updated_str;
  }
//]]>
</script>

補足:yyyy/mm/dd型式にしたい

  var updated_str ="最終更新日:"+ updated.getFullYear()+"/"+zepad(updated.getMonth()+1)+"/"+zepad(updated.getDate());

上記のように書き換えればよいです。

挿入個所

blogger-last-updated02.png

Vaster2|レスポンシブ対応済みのblogger日本語テンプレート」の場合なら、「timestampISO8601」あたりで検索かけるとよいと思う。

<span class='post-tag'>の上あたりに挿入するとOK。

いきなりコードを挿入しないで「■■」とか挿入したい個所に文字を書いて、その文字が期待通りのところに表示されるか?という感じで挿入場所の確認をしたほうがよいかと思います。

参考

Show last updated date in Blogger posts · One Transistor
スポンサーリンク