get_the_modified_date() の使い方|説明・引数・注意点

説明

get_the_modified_date() は、投稿の最終更新日を文字列として返す関数です。

基本構文

get_the_modified_date( string $format = '', int|WP_Post $post = null ): string|int|false

引数

  • $format:日付の書式
  • $post:投稿IDまたはオブジェクト

戻り値

書式化した更新日。

使い方(サンプル)

echo esc_html( get_the_modified_date( 'Y年n月j日' ) );

注意点

  • 公開日と更新日を並べて表示するテンプレートでよく使われます。

実務での使いどころ

  • 記事の「更新日」を表示するとき。
  • 公開日と更新日が違うときだけ、更新日を出すとき。

よくあるミスと対処

  • 一度も更新していない投稿では、公開日と同じ値が返る。
  • 公開日と更新日を、書式を変えずに比べて、判定を誤る。時刻まで比べる書式で取得する。
  • ループの外で、投稿を指定せずに呼んでいる。

NG例

echo get_the_modified_date(); // 未更新でも表示され、公開日と区別がつかない

OK例

if ( get_the_modified_date( 'Ymd' ) !== get_the_date( 'Ymd' ) ) {
	echo esc_html( get_the_modified_date( 'Y年n月j日' ) ) . ' 更新';
}

使い分け早見表

やりたいこと 使うもの
更新日を取得 get_the_modified_date()
公開日を取得 get_the_date()
更新日をそのまま出力 the_modified_date()

関連項目

get_the_date()、the_modified_date()

公式リファレンス:get_the_modified_date() | Function | WordPress Developer Resources

コメント

タイトルとURLをコピーしました