get_attachment_icon_src()
云策文档标注
概述
get_attachment_icon_src() 是一个已弃用的 WordPress 函数,用于根据附件 ID 检索图标的 URL 和文件路径。该函数自 WordPress 2.5.0 起被弃用,建议使用 wp_get_attachment_image_src() 替代。
关键要点
- 函数已弃用:自 WordPress 2.5.0 起,应使用 wp_get_attachment_image_src() 替代。
- 参数:接受 $id(附件 ID,可选,默认 0)和 $fullsize(是否返回完整尺寸图像,可选,默认 false)。
- 返回值:返回一个数组,包含图标的 URL 和完整文件路径;若失败则返回 false。
- 功能逻辑:根据附件类型(缩略图、图像或 MIME 类型图标)动态确定图标源。
- 相关 Hook:apply_filters('icon_dir', string $path) 用于过滤图标目录路径。
代码示例
function get_attachment_icon_src( $id = 0, $fullsize = false ) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image_src()' );
$id = (int) $id;
if ( !$post = get_post($id) )
return false;
$file = get_attached_file( $post->ID );
if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
$src_file = wp_basename($src);
} elseif ( wp_attachment_is_image( $post->ID ) ) {
$src = wp_get_attachment_url( $post->ID );
$src_file = & $file;
} elseif ( $src = wp_mime_type_icon( $post->ID, '.svg' ) ) {
$icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
$src_file = $icon_dir . '/' . wp_basename($src);
}
if ( !isset($src) || !$src )
return false;
return array($src, $src_file);
}注意事项
- 此函数已弃用,新代码中应避免使用,以免影响兼容性。
- 函数内部调用了多个 WordPress 核心函数,如 wp_get_attachment_thumb_url() 和 wp_mime_type_icon(),需确保这些函数可用。
- 返回值可能因附件类型而异,开发时需处理 false 返回值情况。
原文内容
Retrieve icon URL and Path.
Description
See also
Parameters
$idintoptional-
Post ID.
$fullsizebooloptional-
Whether to have full image.
Default:
false
Source
function get_attachment_icon_src( $id = 0, $fullsize = false ) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image_src()' );
$id = (int) $id;
if ( !$post = get_post($id) )
return false;
$file = get_attached_file( $post->ID );
if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
// We have a thumbnail desired, specified and existing.
$src_file = wp_basename($src);
} elseif ( wp_attachment_is_image( $post->ID ) ) {
// We have an image without a thumbnail.
$src = wp_get_attachment_url( $post->ID );
$src_file = & $file;
} elseif ( $src = wp_mime_type_icon( $post->ID, '.svg' ) ) {
// No thumb, no image. We'll look for a mime-related icon instead.
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
$src_file = $icon_dir . '/' . wp_basename($src);
}
if ( !isset($src) || !$src )
return false;
return array($src, $src_file);
}
Hooks
- apply_filters( ‘icon_dir’, string $path )
-
Filters the icon directory path.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Deprecated. Use wp_get_attachment_image_src() |
| 2.1.0 | Introduced. |