勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

WordPressで翌日以降に編集した記事のみ最終更新日を表示する方法

  WordPress CMS

WordPressで翌日以降に編集した記事のみ最終更新日を表示させたい。逆に言うと当日に編集した場合は最終更新日は表示させたくない。というのも記事公開後にページチェックした際、誤字脱字とかが見つかり公開後すぐに編集することがある。その場合公開日と最終更新日が同じ日付で表示されるというのを避けたい。以下に対応方法をメモ。

 

ソースコード

functions.php

//最終更新日の取得
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);
    }
}

最終更新日を表示するページ(single.php等)

<?php $regist_ymd = get_the_time('Y/m/d'); //公開日?>
<?php $update_ymd = get_mtime('Y/m/d'); //最終更新日?>
<?php if( $update_ymd && $regist_ymd != $update_ymd ){ //最終更新日が存在して公開日と最終更新日が異なる場合?>
    <i class="fa fa-repeat"></i>&nbsp;<?php echo $update_ymd;?>
<?php } ?>

 

リファレンス

get_the_time

https://developer.wordpress.org/reference/functions/get_the_time/

get_the_modified_time

https://developer.wordpress.org/reference/functions/get_the_modified_time/

 - WordPress CMS

  関連記事

gzip圧縮とキャッシュをhttpd.confで設定する方法

今まで共用サーバーで使っていたWordPressをVPSに移した。せっかくなので ...

Contact form7で送信数を測定(トラッキングタグ)

contact form7でフォーム送信が完了した時点で計測されるようにトラッキ ...

WordPressでメモリを節約しつつ高速化する設定方法

メモリ1GのVPSを借りてWordpress以外にも、ちょいちょいものを置いてい ...

WordPressにてACFで設定したカスタムフィールドの値を元に記事を並べ替える方法

WordPressにてAdvanced Custom Fieldsで設定したカス ...

WordPressでオリジナルのプラグインを作成する方法

WordPressでオリジナルのプラグインを作成する方法を以下にメモ。 &nbs ...