函数文档

the_guid()

💡 云策文档标注

概述

the_guid() 函数用于显示文章的全局唯一标识符(guid),该标识符经过转义以确保 XML 安全。它不应被用作文章链接,因为博客跨域迁移时可能导致问题。

关键要点

  • 显示文章的全局唯一标识符(guid),输出为转义后的字符串。
  • 参数 $post 可选,可以是文章 ID 或 WP_Post 对象,默认为全局 $post。
  • 通过 apply_filters('the_guid', $post_guid, $post_id) 钩子允许过滤输出。
  • 相关函数包括 get_the_guid()、apply_filters() 和 get_post()。

代码示例

function the_guid( $post = 0 ) {
	$post = get_post( $post );

	$post_guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
	$post_id   = isset( $post->ID ) ? $post->ID : 0;

	/**
	 * Filters the escaped Global Unique Identifier (guid) of the post.
	 *
	 * @since 4.2.0
	 *
	 * @see get_the_guid()
	 *
	 * @param string $post_guid Escaped Global Unique Identifier (guid) of the post.
	 * @param int    $post_id   The post ID.
	 */
	echo apply_filters( 'the_guid', $post_guid, $post_id );
}

注意事项

guid 不应作为文章链接使用,以避免博客跨域迁移时的链接失效问题。


📄 原文内容

Displays the Post Global Unique Identifier (guid).

Description

The guid will appear to be a link, but should not be used as a link to the post. The reason you should not use it as a link, is because of moving the blog across domains.

URL is escaped to make it XML-safe.

Parameters

$postint|WP_Postoptional
Post ID or post object. Default is global $post.

Source

function the_guid( $post = 0 ) {
	$post = get_post( $post );

	$post_guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
	$post_id   = isset( $post->ID ) ? $post->ID : 0;

	/**
	 * Filters the escaped Global Unique Identifier (guid) of the post.
	 *
	 * @since 4.2.0
	 *
	 * @see get_the_guid()
	 *
	 * @param string $post_guid Escaped Global Unique Identifier (guid) of the post.
	 * @param int    $post_id   The post ID.
	 */
	echo apply_filters( 'the_guid', $post_guid, $post_id );
}

Hooks

apply_filters( ‘the_guid’, string $post_guid, int $post_id )

Filters the escaped Global Unique Identifier (guid) of the post.

Changelog

Version Description
1.5.0 Introduced.