get_the_attachment_link()
云策文档标注
概述
get_the_attachment_link() 是一个已弃用的 WordPress 函数,用于获取附件图像的 HTML 内容,并可选包含链接。自 WordPress 2.5.0 起,建议使用 wp_get_attachment_link() 替代。
关键要点
- 函数已弃用:自 WordPress 2.5.0 起,应使用 wp_get_attachment_link() 替代。
- 功能:返回附件图像的 HTML 内容,可控制是否包含链接、使用全尺寸图像或指定最大尺寸。
- 参数:包括 $id(附件 ID)、$fullsize(是否使用全尺寸)、$max_dims(最大尺寸数组)、$permalink(是否包含永久链接)。
- 返回值:字符串类型的 HTML 内容,若附件不存在则返回“Missing Attachment”。
- 内部调用:依赖 get_attachment_innerHTML() 等函数生成 HTML。
代码示例
function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_link()' );
$id = (int) $id;
$_post = get_post($id);
if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
return __('Missing Attachment');
if ( $permalink )
$url = get_attachment_link($_post->ID);
$post_title = esc_attr($_post->post_title);
$innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
return "$innerHTML";
}注意事项
- 此函数已弃用,新代码中应避免使用,改用 wp_get_attachment_link()。
- 使用时需确保传入有效的附件 ID,否则可能返回错误信息。
- 函数内部处理了转义和安全性,如使用 esc_attr() 转义标题。
原文内容
Retrieve HTML content of attachment image with link.
Description
See also
Parameters
$idintoptional-
Post ID.
$fullsizebooloptional-
Whether to use full size image.
Default:
false $max_dimsarrayoptional-
Max image dimensions.
Default:
false $permalinkbooloptional-
Whether to include permalink to image.
Default:
false
Source
function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_link()' );
$id = (int) $id;
$_post = get_post($id);
if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
return __('Missing Attachment');
if ( $permalink )
$url = get_attachment_link($_post->ID);
$post_title = esc_attr($_post->post_title);
$innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
return "<a href='$url' title='$post_title'>$innerHTML</a>";
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Deprecated. Use wp_get_attachment_link() |
| 2.0.0 | Introduced. |