函数文档

the_attachment_link()

💡 云策文档标注

概述

the_attachment_link() 函数用于输出附件页面链接,根据附件类型显示图像或图标。如果附件不存在,会显示“Missing Attachment”字符串。

关键要点

  • 函数输出 HTML 超链接到附件文件或页面,图像附件显示全尺寸或缩略图,非图像附件显示标题文本。
  • 参数包括 $post(可选,附件 ID 或对象)、$fullsize(可选,是否使用全尺寸,默认 false)、$deprecated(可选,已弃用,默认 false)和 $permalink(可选,是否包含永久链接,默认 false)。
  • 如果仅需获取链接而不直接输出,建议使用 wp_get_attachment_link() 替代。

代码示例

function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
    if ( ! empty( $deprecated ) ) {
        _deprecated_argument( __FUNCTION__, '2.5.0' );
    }

    if ( $fullsize ) {
        echo wp_get_attachment_link( $post, 'full', $permalink );
    } else {
        echo wp_get_attachment_link( $post, 'thumbnail', $permalink );
    }
}

注意事项

  • 参数 $deprecated 已弃用,从 WordPress 2.5.0 版本起不再使用,但函数仍会检查并标记弃用。
  • 函数直接输出链接,若需在代码中处理链接字符串,应使用 wp_get_attachment_link()。

📄 原文内容

Displays an attachment page link using an image or icon.

Parameters

$postint|WP_Postoptional
Post ID or post object.
$fullsizebooloptional
Whether to use full size.

Default:false

$deprecatedbooloptional
Deprecated. Not used.

Default:false

$permalinkbooloptional
Whether to include permalink.

Default:false

More Information

Outputs an HTML hyperlink to an attachment file or page, containing either

  1. A full-size image or thumbnail for image attachments; or
  2. The attachment’s title (as text) for non-image attachments

If no attachment can be found, the function displays the string Missing Attachment.

Use wp_get_attachment_link() instead if you just want to get the hyperlink, not print it.

Source

function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '2.5.0' );
	}

	if ( $fullsize ) {
		echo wp_get_attachment_link( $post, 'full', $permalink );
	} else {
		echo wp_get_attachment_link( $post, 'thumbnail', $permalink );
	}
}

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes