the_permalink
云策文档标注
概述
the_permalink 是一个 WordPress 过滤器,用于在 the_permalink() 函数打印前修改当前文章的固定链接 URL。它不适用于 get_permalink() 或 get_the_permalink() 的输出。
关键要点
- 过滤器名称:the_permalink
- 参数:$permalink(当前文章的固定链接字符串)和 $post(文章 ID、WP_Post 对象或 0,默认 0)
- 作用:过滤 the_permalink() 函数输出的链接,允许开发者自定义链接显示
- 注意事项:仅影响 the_permalink(),不影响 get_permalink() 或 get_the_permalink()
代码示例
function append_query_string($url) {
return add_query_arg($_GET, $url);
}
add_filter('the_permalink', 'append_query_string');注意事项
- 从 WordPress 4.4.0 版本开始添加了 $post 参数
- 首次引入于 WordPress 1.5.0 版本
原文内容
Filters the display of the permalink for the current post.
Parameters
Source
echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
Skip to note 2 content
Steven Lin
Example migrated from Codex:
Append the query string for the current page to permalink URLs (uses add_query_arg):
function append_query_string($url) { return add_query_arg($_GET, $url); } add_filter('the_permalink', 'append_query_string');