WordPressのテンプレート賢威7.1で、各記事に表示される日付の表示方法を変更したい!

色々調べて試しているうちに、納得のいく修正方法を見付けたので記録を残しておきたいと思います。

賢威での日付表示のされ方

初めて公開した日が、2018年4月10日だったとします。
その時に、記事にどの様に表示されるかというと、

 2018年4月10日

と、素っ気なく表示されます。

続いて、2018年5月15日に更新したとします。
その時に、記事にどの様に表示されるかというと、

 公開日 : 2018年4月10日 / 更新日 : 2018年5月14日

と表示されていると思います。

この様に表示するための記述が何処にされているかというと...

記事ページの日付表示の修正は、single.php

各記事を出力するためのコードは、 Single.php に書かれています。
その記述は、24~28行目です。

<?php if (get_the_time('Y-m-d') != get_the_modified_date('Y-m-d')) { ?>
 <p class="post-date"><?php _e('Published on','keni') ?> : <time datetime="<?php the_time('Y-m-d'); ?>" itemprop="datePublished" content="<?php the_time('Y-m-d'); ?>" ><?php the_time(get_option('date_format')); ?></time> / <?php _e('Last modified on','keni') ?> : <time datetime="<?php the_modified_date('Y-m-d'); ?>" itemprop="dateModified" content="<?php the_modified_date('Y-m-d'); ?>"><?php echo get_the_modified_date(get_option('date_format')); ?></time></p>
<?php } else { ?>
 <p class="post-date"><time datetime="<?php the_time('Y-m-d'); ?>" itemprop="datePublished" content="<?php the_time('Y-m-d'); ?>" ><?php the_time(get_option('date_format')); ?></time></p>
 <meta itemprop="dateModified" content="<?php the_time('Y-m-d'); ?>">
<?php } ?>

 

どの様な事が書かれているか、ザックリ説明すると、

  1. 公開日と更新日を書き出しなさい
  2. もし更新日情報が無かったら、公開日のみ日付を書き出しなさい

という内容でした。

公開日の表示、または更新日だけ表示したい

記事更新をしていない場合には、公開日のみ。
記事更新をしている場合には、更新日だけ表示したい。

それを実現するためには、以下の部分をちょっと小細工。
赤字にした部分を消去します。

<?php if (get_the_time('Y-m-d') != get_the_modified_date('Y-m-d')) { ?>
 <p class="post-date"><?php _e('Published on','keni') ?> : <time datetime="<?php the_time('Y-m-d'); ?>" itemprop="datePublished" content="<?php the_time('Y-m-d'); ?>" ><?php the_time(get_option('date_format')); ?></time> / <?php _e('Last modified on','keni') ?> : <time datetime="<?php the_modified_date('Y-m-d'); ?>" itemprop="dateModified" content="<?php the_modified_date('Y-m-d'); ?>"><?php echo get_the_modified_date(get_option('date_format')); ?></time></p>
<?php } else { ?>
 <p class="post-date"><time datetime="<?php the_time('Y-m-d'); ?>" itemprop="datePublished" content="<?php the_time('Y-m-d'); ?>" ><?php the_time(get_option('date_format')); ?></time></p>
 <meta itemprop="dateModified" content="<?php the_time('Y-m-d'); ?>">
<?php } ?>

 

修正前は、以下の様に表示されていましたが、

 公開日 : 2018年4月10日 / 更新日 : 2018年5月14日

赤字部分を削除すると、

 更新日 : 2018年5月14日

だけになります。

記事に全ての日付が表示されない様にしたい!

個別記事に表示される日付は、全て表示させたくないという場合もありますよね。

そんな時は、上記のコードを全て削除すれば表示されなくなります。

single.php の変更は、子テーマで

テーマを直接編集すれば変更は出来ますが、失敗すると最悪の場合ブログが表示されなくなってしまいます。

その危険を避けるためにも、子テーマを用意してそこで修正する方が安全です。

子テーマの用意については、こちらの記事を参考にして下さい。

WordPress子テーマの作り方