the_permalink_rss()
云策文档标注
概述
the_permalink_rss() 是一个 WordPress 函数,用于在 RSS 源中显示文章的固定链接。它通过应用过滤器 the_permalink_rss 来允许开发者自定义链接输出。
关键要点
- 函数 the_permalink_rss() 输出经过 esc_url() 清理的当前文章固定链接,适用于 RSS 源。
- 它使用 apply_filters() 调用 the_permalink_rss 过滤器,允许通过 add_filter() 修改链接。
- 相关函数包括 get_permalink() 用于获取链接,esc_url() 用于 URL 清理。
- 该函数自 WordPress 2.3.0 版本引入,被 export_wp() 和 permalink_single_rss() 等函数使用。
代码示例
add_filter('the_permalink_rss', 'rss_campaign_tracking');
function rss_campaign_tracking($post_permalink) {
return $post_permalink . '?utm_source=rss-feed&utm_medium=rss&utm_campaign=feed';
};注意事项
用户贡献笔记中提供了一个示例,展示如何通过添加 Google Analytics 跟踪参数来自定义 RSS 链接,但需注意代码中的转义和安全性。
原文内容
Displays the permalink to the post for use in feeds.
Source
function the_permalink_rss() {
/**
* Filters the permalink to the post for use in feeds.
*
* @since 2.3.0
*
* @param string $post_permalink The current post permalink.
*/
echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
}
Hooks
- apply_filters( ‘the_permalink_rss’, string $post_permalink )
-
Filters the permalink to the post for use in feeds.
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
Skip to note 2 content
James White
Add Google Analytics campaign tracking to RSS permalinks
The following example appends GA query variables to permalinks, to track clicks from a site’s RSS feed to the site itself.
add_filter('the_permalink_rss', 'rss_campaign_tracking'); function rss_campaign_tracking($post_permalink) { return $post_permalink . '?utm_source=rss-feed&utm;_medium=rss&utm;_campaign=feed'; };