函数文档

get_attachment_icon()

💡 云策文档标注

概述

get_attachment_icon() 是一个已弃用的 WordPress 函数,用于获取附件图标的 HTML 内容。它已被 wp_get_attachment_image() 替代,开发者应迁移至新函数。

关键要点

  • 函数已弃用:自 WordPress 2.5.0 起,建议使用 wp_get_attachment_image() 替代。
  • 功能:根据附件 ID 生成图标图像的 HTML 元素,支持尺寸约束。
  • 参数:包括 $id(附件 ID)、$fullsize(是否使用全尺寸图像)和 $max_dims(最大尺寸数组)。
  • 返回值:返回 HTML 字符串或 false(失败时)。
  • 内部处理:调用 get_attachment_icon_src() 获取图标源,应用 attachment_max_dims 过滤器进行尺寸调整。

代码示例

function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
    _deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image()' );
    $id = (int) $id;
    if ( !$post = get_post($id) )
        return false;

    if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
        return false;

    list($src, $src_file) = $src;

    // Do we need to constrain the image?
    if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {

        $imagesize = wp_getimagesize($src_file);

        if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
            $actual_aspect = $imagesize[0] / $imagesize[1];
            $desired_aspect = $max_dims[0] / $max_dims[1];

            if ( $actual_aspect >= $desired_aspect ) {
                $height = $actual_aspect * $max_dims[0];
                $constraint = "width='{$max_dims[0]}' ";
                $post->iconsize = array($max_dims[0], $height);
            } else {
                $width = $max_dims[1] / $actual_aspect;
                $constraint = "height='{$max_dims[1]}' ";
                $post->iconsize = array($width, $max_dims[1]);
            }
        } else {
            $post->iconsize = array($imagesize[0], $imagesize[1]);
            $constraint = '';
        }
    } else {
        $constraint = '';
    }

    $post_title = esc_attr($post->post_title);

    $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";

    return apply_filters( 'attachment_icon', $icon, $post->ID );
}

注意事项

  • 此函数已弃用,新代码应避免使用,改用 wp_get_attachment_image()。
  • 函数内部使用 _deprecated_function() 标记弃用,调用时会触发警告。
  • 依赖多个辅助函数,如 get_attachment_icon_src() 和 wp_getimagesize(),需确保环境支持。

📄 原文内容

Retrieve HTML content of icon attachment image element.

Description

See also

Parameters

$idintoptional
Post ID.
$fullsizebooloptional
Whether to have full size image.

Default:false

$max_dimsarrayoptional
Dimensions of image.

Default:false

Return

string|false HTML content.

Source

function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
	_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image()' );
	$id = (int) $id;
	if ( !$post = get_post($id) )
		return false;

	if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
		return false;

	list($src, $src_file) = $src;

	// Do we need to constrain the image?
	if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {

		$imagesize = wp_getimagesize($src_file);

		if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
			$actual_aspect = $imagesize[0] / $imagesize[1];
			$desired_aspect = $max_dims[0] / $max_dims[1];

			if ( $actual_aspect >= $desired_aspect ) {
				$height = $actual_aspect * $max_dims[0];
				$constraint = "width='{$max_dims[0]}' ";
				$post->iconsize = array($max_dims[0], $height);
			} else {
				$width = $max_dims[1] / $actual_aspect;
				$constraint = "height='{$max_dims[1]}' ";
				$post->iconsize = array($width, $max_dims[1]);
			}
		} else {
			$post->iconsize = array($imagesize[0], $imagesize[1]);
			$constraint = '';
		}
	} else {
		$constraint = '';
	}

	$post_title = esc_attr($post->post_title);

	$icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";

	return apply_filters( 'attachment_icon', $icon, $post->ID );
}

Changelog

Version Description
2.5.0 Deprecated. Use wp_get_attachment_image()
2.0.0 Introduced.