get_comment()获取一个评论的信息

目录

描述

译文

获取一个评论编号并返回评论所属文章的数据库记录。用户可以通过$output参数指定结果的返回方式。

原文

Retrieves comment data given a comment ID or comment object. You can specify, by means of the $output parameter, how you would like the results returned.

If an object is passed then the comment data will be cached and then returned after being passed through a filter. If the comment is empty, then the global comment variable will be used, if it is set.

get_comment() 描述

用法

<?php get_comment$id$output ); ?> 

get_comment() 用法

参数

$comment

(integer) (必填) The ID of the comment you'd like to fetch. You must pass a variable containing an integer (e.g. $id). A literal integer (e.g. 7) will cause a fatal error (Only variables can be passed for reference or Cannot pass parameter 1 by reference).

默认值: None

$output

(string) (可选) How you'd like the result. OBJECT = an object, ARRAY_A = an associative array of keys to values, and ARRAY_N = a numerically indexed array of values.

默认值: OBJECT

get_comment() 参数

注意

get_comment() 注意

历史

添加于 版本: 2.0.0

get_comment() 历史

源文件

get_comment() 函数的代码位于 wp-includes/comment.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieves comment data given a comment ID or comment object.
 *
 * If an object is passed then the comment data will be cached and then returned
 * after being passed through a filter. If the comment is empty, then the global
 * comment variable will be used, if it is set.
 *
 * @since 2.0.0
 *
 * @global wpdb   $wpdb WordPress database abstraction object.
 * @global object $comment
 *
 * @param object|string|int $comment Comment to retrieve.
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
 * @return object|array|null Depends on $output value.
 */
function get_comment(&$comment, $output = OBJECT) {
	global $wpdb;

	if ( empty($comment) ) {
		if ( isset($GLOBALS['comment']) )
			$_comment = & $GLOBALS['comment'];
		else
			$_comment = null;
	} elseif ( is_object($comment) ) {
		wp_cache_add($comment->comment_ID, $comment, 'comment');
		$_comment = $comment;
	} else {
		if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
			$_comment = & $GLOBALS['comment'];
		} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
			$_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
			if ( ! $_comment )
				return null;
			wp_cache_add($_comment->comment_ID, $_comment, 'comment');
		}
	}

	/**
	 * 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 ) {
		$__comment = get_object_vars($_comment);
		return $__comment;
	} elseif ( $output == ARRAY_N ) {
		$__comment = array_values(get_object_vars($_comment));
		return $__comment;
	} else {
		return $_comment;
	}
}

get_comment() 源文件

相关