如果可能,使用图像或图标检索附件页面链接。
原型
wp_get_attachment_link( int|WP_Post $id, string|array $size = 'thumbnail', bool $permalink = false, bool $icon = false, string|false $text = false, array|string $attr = '' )
参数
$id
(int|WP_Post)
(Optional)
帖子ID或帖子对象。
$size
(string|array)
(Optional)
图片尺寸。接受任何有效的图像大小,或以像素为单位的宽度和高度值数组(按此顺序)。
$permalink
(bool)
(Optional)
是否为图像添加永久链接。
$icon
(bool)
(Optional)
附件是否是图标。
$text
(string|false)
(Optional)
链接要使用的文本。通过传递字符串激活,否则为false。
$attr
(array|string)
(Optional)
数组或属性字符串。
返回值
(string)
HTML内容。
源文件
路径:wp-includes/post-template.php
<?php
...
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
$_post = get_post( $id );
if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) ) {
return __( 'Missing Attachment' );
}
if ( $permalink ) {
$url = get_attachment_link( $_post->ID );
}
if ( $text ) {
$link_text = $text;
} elseif ( $size && 'none' != $size ) {
$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
} else {
$link_text = '';
}
if ( '' === trim( $link_text ) ) {
$link_text = $_post->post_title;
}
if ( '' === trim( $link_text ) ) {
$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
}
/**
* Filters a retrieved attachment page link.
*
* @since 2.7.0
*
* @param string $link_html The page link HTML output.
* @param int $id Post ID.
* @param string|array $size Size of the image. Image size or array of width and height values (in that order).
* Default 'thumbnail'.
* @param bool $permalink Whether to add permalink to image. Default false.
* @param bool $icon Whether to include an icon. Default false.
* @param string|bool $text If string, will be link text. Default false.
*/
return apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_get_attachment_link/