函数文档

get_comment_guid()

💡 云策文档标注

概述

get_comment_guid() 函数用于获取当前评论的 feed GUID。它基于评论对象或 ID 生成一个唯一的标识符字符串。

关键要点

  • 参数 $comment_id 可选,可以是评论对象或 ID,默认为全局评论对象,默认值为 null。
  • 返回值:成功时返回评论的 GUID 字符串,失败时返回 false。
  • 函数内部调用 get_comment() 获取评论数据,然后结合 get_the_guid() 和评论 ID 构建 GUID。

代码示例

function get_comment_guid( $comment_id = null ) {
    $comment = get_comment( $comment_id );

    if ( ! is_object( $comment ) ) {
        return false;
    }

    return get_the_guid( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
}

注意事项

  • 如果 $comment_id 无效或无法获取评论对象,函数将返回 false。
  • GUID 格式为文章 GUID 加上 '#comment-' 和评论 ID,确保唯一性。

📄 原文内容

Retrieves the feed GUID for the current comment.

Parameters

$comment_idint|WP_Commentoptional
Optional comment object or ID. Defaults to global comment object.

Default:null

Return

string|false GUID for comment on success, false on failure.

Source

function get_comment_guid( $comment_id = null ) {
	$comment = get_comment( $comment_id );

	if ( ! is_object( $comment ) ) {
		return false;
	}

	return get_the_guid( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
}

Changelog

Version Description
2.5.0 Introduced.