get_the_guid()
云策文档标注
概述
get_the_guid() 函数用于检索文章的全局唯一标识符(guid),该标识符通常显示为链接形式,但不应用于实际链接,以避免跨域迁移博客时出现问题。
关键要点
- 函数返回文章的 guid,参数为可选的 $post(整数或 WP_Post 对象),默认使用全局 $post。
- guid 不应作为文章链接使用,因为博客迁移到不同域名时可能导致链接失效。
- 函数内部通过 get_post() 获取文章数据,并应用 'get_the_guid' 过滤器进行自定义处理。
代码示例
function get_the_guid( $post = 0 ) {
$post = get_post( $post );
$post_guid = isset( $post->guid ) ? $post->guid : '';
$post_id = isset( $post->ID ) ? $post->ID : 0;
/**
* Filters the Global Unique Identifier (guid) of the post.
*
* @since 1.5.0
*
* @param string $post_guid Global Unique Identifier (guid) of the post.
* @param int $post_id The post ID.
*/
return apply_filters( 'get_the_guid', $post_guid, $post_id );
}注意事项
- guid 主要用于内部标识,如 RSS 馈送,而非前端导航链接。
- 使用 apply_filters('get_the_guid', $post_guid, $post_id) 钩子可以自定义 guid 输出。
- 相关函数包括 the_guid()(显示 guid)和 get_comment_guid()(检索评论的 guid)。
原文内容
Retrieves the Post Global Unique Identifier (guid).
Description
The guid will appear to be a link, but should not be used as an link to the post. The reason you should not use it as a link, is because of moving the blog across domains.
Parameters
$postint|WP_Postoptional-
Post ID or post object. Default is global $post.
Source
function get_the_guid( $post = 0 ) {
$post = get_post( $post );
$post_guid = isset( $post->guid ) ? $post->guid : '';
$post_id = isset( $post->ID ) ? $post->ID : 0;
/**
* Filters the Global Unique Identifier (guid) of the post.
*
* @since 1.5.0
*
* @param string $post_guid Global Unique Identifier (guid) of the post.
* @param int $post_id The post ID.
*/
return apply_filters( 'get_the_guid', $post_guid, $post_id );
}
Hooks
- apply_filters( ‘get_the_guid’, string $post_guid, int $post_id )
-
Filters the Global Unique Identifier (guid) of the post.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |