返回介绍

get_comment()

发布于 2017-09-10 23:01:40 字数 9507 浏览 1130 评论 0 收藏 0

get_comment( WP_Comment|string|int $comment = null,  string $output = OBJECT )

Retrieves comment data given a comment ID or comment object.


description

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.


参数

$comment

(WP_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 WP_Comment object, an associative array, or a numeric array, respectively.

Default value: OBJECT


返回值

(WP_Comment|array|null) Depends on $output value.


源代码

File: wp-includes/comment.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;
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-comment.php: WP_Comment::__construct()
  • wp-includes/class-wp-comment.php: WP_Comment::get_instance()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/comment.php: get_comment

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::get_comment()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::delete_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::create_item()
  • wp-includes/class-wp-comment-query.php: WP_Comment_Query::fill_descendants()
  • wp-includes/comment.php: wp_handle_comment_submission()
  • wp-includes/comment.php: wp_new_comment_notify_moderator()
  • wp-includes/comment.php: wp_new_comment_notify_postauthor()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::floated_admin_avatar()
  • wp-includes/class-wp-comment-query.php: WP_Comment_Query::get_comments()
  • wp-includes/link-template.php: get_avatar_data()
  • wp-admin/includes/template.php: touch_time()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_delete_comment_response()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dim_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_comments()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_edit_comment()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_comment()
  • wp-admin/includes/comment.php: get_comment_to_edit()
  • wp-admin/includes/comment.php: floated_admin_avatar()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/pluggable.php: get_avatar()
  • wp-includes/pluggable.php: wp_notify_postauthor()
  • wp-includes/pluggable.php: wp_notify_moderator()
  • wp-includes/deprecated.php: get_commentdata()
  • wp-includes/functions.php: wp_scheduled_delete()
  • wp-includes/link-template.php: get_edit_comment_link()
  • wp-includes/link-template.php: edit_comment_link()
  • wp-includes/feed.php: get_comment_guid()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deleteComment()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_editComment()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getComment()
  • wp-includes/comment-template.php: comment_form_title()
  • wp-includes/comment-template.php: get_comment_reply_link()
  • wp-includes/comment-template.php: get_comment_link()
  • wp-includes/comment-template.php: get_comment_text()
  • wp-includes/comment-template.php: comment_text()
  • wp-includes/comment-template.php: get_comment_time()
  • wp-includes/comment-template.php: get_comment_type()
  • wp-includes/comment-template.php: comment_author_url()
  • wp-includes/comment-template.php: get_comment_class()
  • wp-includes/comment-template.php: get_comment_date()
  • wp-includes/comment-template.php: get_comment_excerpt()
  • wp-includes/comment-template.php: comment_excerpt()
  • wp-includes/comment-template.php: get_comment_ID()
  • wp-includes/comment-template.php: get_comment_author()
  • wp-includes/comment-template.php: comment_author()
  • wp-includes/comment-template.php: get_comment_author_email()
  • wp-includes/comment-template.php: comment_author_email()
  • wp-includes/comment-template.php: get_comment_author_email_link()
  • wp-includes/comment-template.php: get_comment_author_link()
  • wp-includes/comment-template.php: get_comment_author_IP()
  • wp-includes/comment-template.php: get_comment_author_url()
  • wp-includes/comment.php: wp_set_comment_status()
  • wp-includes/comment.php: wp_update_comment()
  • wp-includes/comment.php: wp_spam_comment()
  • wp-includes/comment.php: wp_unspam_comment()
  • wp-includes/comment.php: wp_get_comment_status()
  • wp-includes/comment.php: wp_insert_comment()
  • wp-includes/comment.php: get_page_of_comment()
  • wp-includes/comment.php: wp_delete_comment()
  • wp-includes/comment.php: wp_trash_comment()
  • wp-includes/comment.php: wp_untrash_comment()
  • Show 59 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example
    To get the author’s name of a comment with ID 7:

    
    <?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'];
    ?>
    
    
    <?php
    //	Correct: pass a dummy variable as post_id
    $the_comment = & get_comment( $dummy_id = 7 );
    	
    //	Incorrect: literal integer as post_id
    $the_comment = & get_comment( 7 );
    //	Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
    ?>
    
    
  2. Example
    To get the author’s name of a comment with ID 7:

    
    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id ); 
    $name = $comment_id_7->comment_author;
    ?>
    

    Alternatively, specify the $output parameter:

    
    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id, ARRAY_A );
    $name = $comment_id_7['comment_author'];
    ?>
    
    
    <?php
    //	Correct: pass a dummy variable as post_id
    $the_comment = & get_comment( $dummy_id = 7 );
    	
    //	Incorrect: literal integer as post_id
    $the_comment = & get_comment( 7 );
    //	Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文