在给定注释ID或注释对象的情况下检索注释数据。
原型
get_comment( WP_Comment|string|int $comment = null, string $output = OBJECT )
描述
如果传递了一个对象,那么注释数据将被缓存,然后在通过过滤器后返回。如果注释为空,则将使用全局注释变量(如果已设置)。
参数
$comment
(WP_Comment|string|int)
(Optional)
评论要检索。
$output
(string)
(Optional)
所需的返回类型。 OBJECT,ARRAY_A或ARRAY_N之一,分别对应于WP_Comment对象,关联数组或数值数组。
返回值
(WP_Comment|array|null)
取决于$产值。
源文件
路径:wp-includes/comment.php
<?php
...
function get_comment( &$comment = null, $output = OBJECT ) {
if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
$comment = $GLOBALS['comment'];
}
if ( $comment instanceof WP_Comment ) {
$_comment = $comment;
} elseif ( is_object( $comment ) ) {
$_comment = new WP_Comment( $comment );
} else {
$_comment = WP_Comment::get_instance( $comment );
}
if ( ! $_comment ) {
return null;
}
/**
* Fires after a comment is retrieved.
*
* @since 2.3.0
*
* @param mixed $_comment Comment data.
*/
$_comment = apply_filters( 'get_comment', $_comment );
if ( $output == OBJECT ) {
return $_comment;
} elseif ( $output == ARRAY_A ) {
return $_comment->to_array();
} elseif ( $output == ARRAY_N ) {
return array_values( $_comment->to_array() );
}
return $_comment;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_comment/