
賢威7のカスタマイズの備忘録です。
functions.phpに記事更新日取得の関数を追加する
賢威7.0カスタマイズで最終更新日を表示させる方法です。私が持っているバージョンは賢威7.0.7.7ですのでバージョンに合わせた方法を紹介します。※賢威バージョン確認方法:外観→テーマ→賢威のテーマ詳細
カスタマイズ時のバージョンは「賢威7.0.7.7 クール NAVY WordPress版」です。
fanctions.phpを修正します。ファイルによってはバージョンによってファイル名が違うことがありますが、このファイルは変わっていませんね。
1 2 3 4 5 6 7 8 9 10 11 12 |
//記事更新日取得 function get_mtime($format) { $mtime = get_the_modified_time('Ymd'); $ptime = get_the_time('Ymd'); if ($ptime > $mtime) { return get_the_time($format); } elseif ($ptime === $mtime) { return null; } else { return get_the_modified_time($format); } } |
WordPressダッシュボードから「外観」⇒「テーマの編集」をクリックして、画面右側のテンプレートリストより「functions.php」を選択します。
記事更新日を取得する関数を、functions.phpに記述します。上記内容をコピペすればOKです。※必ず<php?と最後の?>の間に記述するようにしてください。
表示させたいphpファイルにコードを記述する
主な表示箇所
- 記事一覧 > cont.php
- 個別記事 > single.php
次に、記事更新日を表示させたい箇所にコードを追加していきます。
1 2 |
/*更新日表示コード*/ <?php if ($mtime = get_mtime('Y-m-d')) echo '更新日:', $mtime; ?> |
更新日を表示させるコードを、任意の箇所に記述します。
記事一覧に更新日を表示(cont.php)
/*追加前*/
<p class=”post–date“><time datetime=”<?php the_time(‘Y-m-d’); ?>”><?php the_time(get_option(‘date_format’)); ?></time></p>
cont.phpの19行目あたりが、投稿日を表示箇所なので、その隣りに追加してみましょう。
1 |
/*追加後*/
<p class=”post–date“>投稿日:<?php the_time(get_option( ‘date_format‘)); //the_time(‘Y–m–d‘) ?></span>│更新日:<span class=”post–data“><?php the_modified_date(‘Y–m–d‘) ?></p>
1 |
正しく追加すると記事一覧に以下のように表示されるようになります。
個別記事に更新日を表示(single.php)
個別記事も記事一覧とほぼ変わりません。先ほどのコードをsingle.phpに記述していきます。
/*追加前*/
<p class=”post–date“><time datetime=”<?php the_time(‘Y-m-d’); ?>”><?php the_time(get_option(‘date_format’)); ?></time></p>
1 |
/*追加後*/
<p class=”post–date“>投稿日:<?php the_time(get_option( ‘date_format‘)); //the_time(‘Y–m–d‘) ?></span>│更新日:<span class=”post–data“><?php the_modified_date(‘Y–m–d‘) ?></p>
1 |
正しく追加すると個別記事に以下のように表示されるようになります。
まとめ
cont.phpもsingle.phpも中間程度にコピペするので、全体の記述の流れがわかっていないうちはもちろん、慣れた後でも必ず、バックアップをしっかりとりましょう