钩子文档

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

$permalinkstring
The permalink for the current post.
$postint|WP_Post
Post ID, WP_Post object, or 0. Default 0.

More Information

the_permalink is a filter applied to the permalink URL for a post prior to printing by the function the_permalink().

Note: The output of the functions get_permalink() or get_the_permalink() is not filtered.

Source

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );

Changelog

Version Description
4.4.0 Added the $post parameter.
1.5.0 Introduced.

User Contributed Notes