get_comment_date()

関数の概要

get_comment_date()は、WordPressで特定のコメントの公開日を取得するための関数です。コメントの公開日をフォーマットして返します。この関数は、コメントの公開日をカスタム表示したい場合に便利です。

パラメータの説明

get_comment_date()関数は3つのパラメータを受け取ります。

  1. $format (string, オプション): 日付のフォーマット。指定しない場合、サイト全体のデフォルトフォーマットが使用されます。
  2. $comment_ID (int|WP_Comment, オプション): コメントのIDまたはWP_Commentオブジェクト。省略された場合、現在のコメントが対象になります。
  3. $gmt (bool, オプション): trueの場合、GMT日付を取得します。デフォルトはfalseです。

使用例

以下は、get_comment_date()関数を使用してコメントの公開日を取得し表示する例です。

<?php
// コメントの公開日を取得する
$comment_id = 123; // 例: コメントIDが123
$comment_date = get_comment_date('F j, Y', $comment_id);

echo '<p>Comment was published on: ' . esc_html($comment_date) . '</p>';
?>

現在のコメントの公開日を取得する例(コメントループ内)

<?php
// コメントループ内で使用
if (have_comments()) {
    wp_list_comments(array(
        'callback' => function($comment, $args, $depth) {
            $comment_date = get_comment_date('F j, Y', $comment);
            echo '<li>';
            echo '<p>Comment by ' . get_comment_author() . ' on ' . esc_html($comment_date) . '</p>';
            comment_text();
            echo '</li>';
        }
    ));
}
?>

カスタマイズ

get_comment_date()関数を使用すると、日付のフォーマットをカスタマイズできます。以下は、コメントの日付を「年月日」形式で表示する例です。

<?php
$comment_id = 123; // 例: コメントIDが123
$comment_date = get_comment_date('Y年m月d日', $comment_id);

echo '<p>コメントは ' . esc_html($comment_date) . ' に公開されました。</p>';
?>

関連する関数

  • the_comment_date(): 現在のコメントの公開日を表示します。
  • get_comment_time(): コメントの公開時間を取得します。
  • get_comment_author(): コメントの著者名を取得します。
  • get_comment_link(): コメントのURLを取得します。
  • comment_text(): コメントの内容を表示します。