get_comment_author

函式
get_comment_author ( $comment_ID = 0 )
引數
  • (int|WP_Comment) $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author. Default current comment.
    Required:
返回值
  • (string) The comment author
定義位置
相關方法
get_comment_author_ipget_comment_author_rssget_comment_author_urlget_comment_author_linkcomment_author
引入
1.5.0
棄用
-

get_comment_author: 這個函式返回一個評論的作者的名字。它需要一個引數,$comment_ID,即你想檢索作者名字的評論的ID。

檢索當前評論的作者。

如果該評論有一個空的comment_author欄位,則假定為”Anonymous”人。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_comment_author( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
$comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_ID;
if ( empty( $comment->comment_author ) ) {
$user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
if ( $user ) {
$author = $user->display_name;
} else {
$author = __( 'Anonymous' );
}
} else {
$author = $comment->comment_author;
}
/**
* Filters the returned comment author name.
*
* @since 1.5.0
* @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
*
* @param string $author The comment author's username.
* @param string $comment_ID The comment ID as a numeric string.
* @param WP_Comment $comment The comment object.
*/
return apply_filters( 'get_comment_author', $author, $comment_ID, $comment );
}
function get_comment_author( $comment_ID = 0 ) { $comment = get_comment( $comment_ID ); $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_ID; if ( empty( $comment->comment_author ) ) { $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false; if ( $user ) { $author = $user->display_name; } else { $author = __( 'Anonymous' ); } } else { $author = $comment->comment_author; } /** * Filters the returned comment author name. * * @since 1.5.0 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. * * @param string $author The comment author's username. * @param string $comment_ID The comment ID as a numeric string. * @param WP_Comment $comment The comment object. */ return apply_filters( 'get_comment_author', $author, $comment_ID, $comment ); }
function get_comment_author( $comment_ID = 0 ) {
	$comment    = get_comment( $comment_ID );
	$comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_ID;

	if ( empty( $comment->comment_author ) ) {
		$user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
		if ( $user ) {
			$author = $user->display_name;
		} else {
			$author = __( 'Anonymous' );
		}
	} else {
		$author = $comment->comment_author;
	}

	/**
	 * Filters the returned comment author name.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
	 *
	 * @param string     $author     The comment author's username.
	 * @param string     $comment_ID The comment ID as a numeric string.
	 * @param WP_Comment $comment    The comment object.
	 */
	return apply_filters( 'get_comment_author', $author, $comment_ID, $comment );
}

常見問題

FAQs
檢視更多 >