描述:
通过id获取附件(特色图像/文件)的链接
用法:
<?php wp_get_attachment_link( $id, $size, $permalink, $icon, $text ); ?>
参数:
$id
(integer) (可选) 附件ID
默认值: 当前文章ID
$size
(string/array) (可选) 图像大小。字符串关键字(缩略图、中、大或全)或以像素表示宽度和高度的两项数组,例如数组(32,32)。从2.5版起,此参数不影响媒体图标的大小,媒体图标始终以其原始大小显示。
默认值: ‘medium’
$permalink
(boolean) (可选) 直接链接到附件文件/图像(默认)或附件页。
默认值: ‘False’
$icon
(boolean) (可选) 使用媒体图标表示附件。
默认值: ‘False’
$text
(string/boolean) (可选) 显示附件的文本链接。
默认值: ‘false’
示例:
<?php
$id = 9; // ID of an attachment
echo wp_get_attachment_link( $id, 'medium' );
?>
源文件:
/**
* Retrieve an attachment page link using an image or icon, if possible.
*
* @since 2.5.0
*
* @param int|WP_Post $id Optional. Post ID or post object.
* @param string $size Optional, default is 'thumbnail'. Size of image, either array or string.
* @param bool $permalink Optional, default is false. Whether to add permalink to image.
* @param bool $icon Optional, default is false. Whether to include icon.
* @param string|bool $text Optional, default is false. If string, then will be link text.
* @param array|string $attr Optional. Array or string of attributes.
* @return string HTML content.
*/
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
$id = intval( $id );
$_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( $id, $size, $icon, $attr );
} else {
$link_text = '';
}
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
/**
* Filter 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 $size Image size. 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', "$link_text", $id, $size, $permalink, $icon, $text );
}
发表评论
还没有评论,快来抢沙发吧!