WordPressで記事に最終更新日を付ける方法

  • 投稿 : 2013-05-24
  • 更新 : 2014-10-09

ブログ記事は日付が重要?!

他の人の記事を読んでいても、いつごろ書かれた記事なのかを知りたいことがあります。そういう時に目安となる日付がないとわからないんですね。

情報系でなくても、その当時のムードみたいなものと関連している感想や出来事なんかもあるので、2年以上たつとやはり逆に日付が意味を持つ気がします。

しかし、私の場合は、書き手になると日付を書くと古い記事がそのまま読み飛ばされるような気がして、心理的に嫌な気がするわけです。

ということで、最終更新日付も付ければよいのでは?いうことで対応してみました。

更新日の有効性を判断する関数

function is_mtime() {
    $mtime = get_the_modified_time('Ymd');
    $ptime = get_the_time('Ymd');
    if ($ptime >= $mtime) {
        return false;
    } else {
        return true;
    }
}

これをテンプレートのfunctions.phpに追加してください

補足

get_the_time() //投稿日
get_the_modified_time() //更新日

投稿日>=更新日(投稿日のほうが新しい)時は、更新日を表示する意味がないのでそれを返す関数
true :有効
false:無効

※「参考にしたサイト」を参考
予約投稿とかすると日付が逆転するみたいです。

サンプル その1

■更新がない時の表示
公開日:2013年5月20日
■更新された時の表示
公開日:2013年5月20日 最終更新日:2013年5月24日

公開日:<time class="entry-date" datetime="<?php the_time('c') ;?>"><?php the_time('Y年n月j日') ;?></time> 
<?php if(is_mtime()){ ?>
最終更新日: <time class="last-modified-date" datetime="<?php echo get_the_modified_time('c'); ?>"><?php echo get_the_modified_time('Y年n月j日'); ?></time>
<?php } ?>

テンプレートの表示したい箇所に貼り付けてください。
twentytwelveなら
content.phpか
function.phpの「function twentytwelve_entry_meta() 」
あたりです

サンプル その2

最終更新日:2013年5月20日

投稿日、更新日の区別なくすべて最終更新日として表示する
  <?php if (is_mtime()) { ?>
    最終更新日:<time class="last-modified-date" datetime="<?php echo get_the_modified_time('c'); ?>"><?php echo get_the_modified_time('Y年n月j日'); ?></time>
  <?php }else{ ?>
    最終更新日:<time class="last-modified-date" datetime="<?php the_time('c'); ?>"><?php the_time('Y年n月j日'); ?></time>
  <?php } ?>

参考にしたサイト

WordPressで記事の最終更新日を取得 | firegoby
WordPressで最終更新日を表示する方法(予約投稿にも対応) | WP SEOブログ
WordPressで記事の最終更新日を表示する方法 HTML5対応版

上記サイトと本記事で自分好みにカスタマイズ可能だと思います。

スポンサーリンク