WordPress | 記事の公開日付や更新日付を取得する方法

PHP
スポンサーリンク

WordPress では記事の 公開日付更新日付 をテンプレートタグで簡単に取得できます。テーマの投稿ループ内でよく使われる方法を整理します。


公開日付を取得する方法

1. 出力する(echo)

<?php the_time('Y年n月j日'); ?>
PHP
  • the_time() は記事の公開日時を 直接出力 します。
  • フォーマットは PHP の date() と同じ書式指定が可能。
    例: Y-m-d H:i2025-11-14 13:56

2. 文字列として取得する

<?php echo get_the_date('Y年n月j日'); ?>
PHP
  • get_the_date()文字列を返すので、変数に格納したり加工できます。

更新日付を取得する方法

1. 出力する

<?php the_modified_date('Y年n月j日'); ?>
PHP
  • 記事が更新された日付を 直接出力

2. 文字列として取得する

<?php echo get_the_modified_date('Y年n月j日'); ?>
PHP
  • 更新日付を 文字列で取得

公開日付と更新日付を両方表示する例

<p class="post-date">
  公開日: <?php echo get_the_date('Y年n月j日'); ?><br>
  更新日: <?php echo get_the_modified_date('Y年n月j日'); ?>
</p>
PHP

実用的なカスタマイズ例

  • 公開日と更新日が同じ場合は「更新日」を非表示にする:
<?php if ( get_the_date() !== get_the_modified_date() ) : ?>
  <p>更新日: <?php echo get_the_modified_date('Y年n月j日'); ?></p>
<?php endif; ?>
PHP
  • 公開日を「〇日前」と相対表示するには human_time_diff() を利用:
<?php
echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . '前';
?>
PHP

✅ まとめ

  • 公開日 → the_time() / get_the_date()
  • 更新日 → the_modified_date() / get_the_modified_date()
  • 条件分岐や human_time_diff() を組み合わせると、より柔軟な表示が可能です。
PHPWeb
スポンサーリンク
シェアする
@lifehackerをフォローする
スポンサーリンク
タイトルとURLをコピーしました