get_lastpostmodified()


WordPressのget_lastpostmodified()関数は、最後に変更された投稿またはページの日付と時刻を取得するために使用されます。

シンタックス

get_lastpostmodified( string $timezone = 'server', string $type = 'post' );

引数の説明:

  • $timezone (string) — 時刻のタイムゾーン。デフォルト値は'server'です。オプション値: 'server''gmt'
  • $type (string) — 変更日を取得する投稿タイプ。デフォルト値は'post'です。例えば'post''page''any'

戻り値:

  • string — 最後に変更された投稿の日付と時刻。

例 1: 最後に変更された投稿の日付の取得

<?php echo get_lastpostmodified(); ?>

例 2: GMTタイムゾーンでの取得

$timezone引数を'gmt'に設定してGMTの時刻を取得します。

<?php echo get_lastpostmodified( 'gmt' ); ?>

例 3: カスタム投稿タイプの変更日取得

$type引数を設定してカスタム投稿タイプの変更日を取得します。

<?php echo get_lastpostmodified( 'server', 'custom_post_type' ); ?>

例 4: 投稿の最後の変更日を表示する

テーマのフッターやヘッダー内で使用できます。

<?php
$last_modified = get_lastpostmodified();
if ( $last_modified ) {
    echo '最後の変更日: ' . $last_modified;
}
?>

例 5: 条件付きで投稿の最終変更日を取得

ifステートメントを使用して、日付が存在する場合のみ表示します。

<?php
if ( get_lastpostmodified() ) {
    echo '最終更新: ' . get_lastpostmodified( 'server' );
} else {
    echo '更新情報はありません。';
}
?>

注意事項:

  • get_lastpostmodified()は、主に投稿やページの最終変更日時を表示するために使われます。
  • 正確な時刻を表示する場合、'server''gmt'の違いを理解して使用してください。

関連機能: