wp_get_attachment_url()
云策文档标注
概述
wp_get_attachment_url() 函数用于获取附件的 URL,支持通过 wp_get_attachment_url 过滤器修改输出。注意该函数不会对 URL 进行编码,若附件文件名包含无效字符,需手动处理。
关键要点
- 参数 $attachment_id 可选,默认为全局 $post,返回字符串或 false
- 可通过 wp_get_attachment_url 过滤器自定义 URL 输出
- 函数不进行 URL 编码,需使用 rawurlencode() 处理文件名
- 提供示例代码获取根相对 URL 并编码文件名
- 相关函数包括 get_attachment_link、wp_insert_attachment 等
代码示例
$parsed = parse_url( wp_get_attachment_url( $attachment->ID ) );
$url = dirname( $parsed['path'] ) . '/' . rawurlencode( basename( $parsed['path'] ) );注意事项
- 若需附件页面 URI 而非文件本身,使用 get_attachment_link
- 在 SSL 前端非管理页面自动转换为 HTTPS
- 内部处理基于上传目录和元数据,兼容旧版本 GUID 回退
原文内容
Retrieves the URL for an attachment.
Parameters
$attachment_idintoptional-
Attachment post ID. Defaults to global $post.
Source
function wp_get_attachment_url( $attachment_id = 0 ) {
global $pagenow;
$attachment_id = (int) $attachment_id;
$post = get_post( $attachment_id );
if ( ! $post ) {
return false;
}
if ( 'attachment' !== $post->post_type ) {
return false;
}
$url = '';
// Get attached file.
$file = get_post_meta( $post->ID, '_wp_attached_file', true );
if ( $file ) {
// Get upload directory.
$uploads = wp_get_upload_dir();
if ( $uploads && false === $uploads['error'] ) {
// Check that the upload base exists in the file location.
if ( str_starts_with( $file, $uploads['basedir'] ) ) {
// Replace file location with url location.
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
} elseif ( str_contains( $file, 'wp-content/uploads' ) ) {
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
} else {
// It's a newly-uploaded file, therefore $file is relative to the basedir.
$url = $uploads['baseurl'] . "/$file";
}
}
}
/*
* If any of the above options failed, Fallback on the GUID as used pre-2.7,
* not recommended to rely upon this.
*/
if ( ! $url ) {
$url = get_the_guid( $post->ID );
}
// On SSL front end, URLs should be HTTPS.
if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
$url = set_url_scheme( $url );
}
/**
* Filters the attachment URL.
*
* @since 2.1.0
*
* @param string $url URL for the given attachment.
* @param int $attachment_id Attachment post ID.
*/
$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
if ( ! $url ) {
return false;
}
return $url;
}
Hooks
- apply_filters( ‘wp_get_attachment_url’, string $url, int $attachment_id )
-
Filters the attachment URL.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 3 content
Codex
Basic Example
Outputs something like http://example.net/wp-content/uploads/filename
Skip to note 4 content
Codex
Use Post Thumbnail as Background Image
if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( has_post_thumbnail() ) { $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() ); echo '<div style="background-image:url('.$feat_image_url.');"></div>'; } endwhile; endif;