attachment_link
云策文档标注
概述
attachment_link 是一个 WordPress 过滤器,用于修改附件的固定链接。它允许开发者在输出附件链接时自定义其 URL。
关键要点
- 过滤器名称:attachment_link
- 参数:$link(附件固定链接字符串)和 $post_id(附件 ID)
- 核心用途:过滤附件的 permalink,常用于自定义附件页面链接或重定向
代码示例
add_filter( 'attachment_link', 'custom_attachment_link', 10, 2 );
function custom_attachment_link( $link, $post_id ) {
// 自定义逻辑,例如重定向到媒体文件本身
$attachment_url = wp_get_attachment_url( $post_id );
return $attachment_url ? $attachment_url : $link;
}注意事项
- 从 WordPress 5.6.0 开始,返回空字符串可以禁用媒体模态框中的“查看附件页面”链接。
- 该过滤器自 WordPress 2.0.0 引入,兼容性良好。
原文内容
Filters the permalink for an attachment.
Parameters
$linkstring-
The attachment’s permalink.
$post_idint-
Attachment ID.
Source
return apply_filters( 'attachment_link', $link, $post->ID );