get_comment()
最后更新于:2021-11-26 07:50:21
get_comment( GC_Comment|string|int$comment=null, string$output=OBJECT)Retrieves comment data given a comment ID or comment object.
参数
- $comment
-
(GC_Comment|string|int) (Optional) Comment to retrieve.
Default value: null
- $output
-
(string) (Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a GC_Comment object, an associative array, or a numeric array, respectively.
Default value: OBJECT
响应
(GC_Comment|array|null) Depends on $output value.
源文件
文件: gc-includes/comment.php
function get_comment( $comment = null, $output = OBJECT ) {
if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
$comment = $GLOBALS['comment'];
}
if ( $comment instanceof GC_Comment ) {
$_comment = $comment;
} elseif ( is_object( $comment ) ) {
$_comment = new GC_Comment( $comment );
} else {
$_comment = GC_Comment::get_instance( $comment );
}
if ( ! $_comment ) {
return null;
}
/**
* Fires after a comment is retrieved.
*
* @since 2.3.0
*
* @param GC_Comment $_comment Comment data.
*/
$_comment = apply_filters( 'get_comment', $_comment );
if ( OBJECT === $output ) {
return $_comment;
} elseif ( ARRAY_A === $output ) {
return $_comment->to_array();
} elseif ( ARRAY_N === $output ) {
return array_values( $_comment->to_array() );
}
return $_comment;
}
<?php $my_id = 7; $comment_id_7 = get_comment( $my_id ); $name = $comment_id_7->comment_author; ?> Alternatively, specify the <code>$output</code> parameter: <?php $my_id = 7; $comment_id_7 = get_comment( $my_id, ARRAY_A ); $name = $comment_id_7['comment_author']; ?>