get_comment_time()

関数の概要

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

パラメータの説明

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

  1. $format (string, オプション): 時間のフォーマット。指定しない場合、サイト全体のデフォルトフォーマットが使用されます。
  2. $gmt (bool, オプション): trueの場合、GMT時間を取得します。デフォルトはfalseです。
  3. $translate (bool, オプション): trueの場合、翻訳された時間を取得します。デフォルトはtrueです。

使用例

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

<?php
// コメントの公開時間を取得する
$comment_id = 123; // 例: コメントIDが123
$comment_time = get_comment_time('g:i a', false, true, $comment_id);

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

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

<?php
// コメントループ内で使用
if (have_comments()) {
    wp_list_comments(array(
        'callback' => function($comment, $args, $depth) {
            $comment_time = get_comment_time('g:i a', false, true, $comment);
            echo '<li>';
            echo '<p>Comment by ' . get_comment_author() . ' at ' . esc_html($comment_time) . '</p>';
            comment_text();
            echo '</li>';
        }
    ));
}
?>

カスタマイズ

get_comment_time()関数を使用すると、時間のフォーマットをカスタマイズできます。以下は、コメントの時間を24時間形式で表示する例です。

<?php
$comment_id = 123; // 例: コメントIDが123
$comment_time = get_comment_time('H:i', false, true, $comment_id);

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

関連する関数

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