钩子文档

wp_get_attachment_image

💡 云策文档标注

概述

wp_get_attachment_image 是一个 WordPress 过滤器,用于修改表示图像附件的 HTML img 元素。它允许开发者在图像输出前自定义其 HTML 属性。

关键要点

  • 过滤器名称:wp_get_attachment_image
  • 参数:$html(HTML img 元素或失败时的空字符串)、$attachment_id(图像附件 ID)、$size(图像尺寸)、$icon(是否作为图标处理)、$attr(属性数组)
  • 属性数组 $attr 可包含 src、class、alt、srcset、sizes、loading、decoding、fetchpriority 等键值对
  • 源调用:apply_filters('wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr)

注意事项

  • 此过滤器在 WordPress 5.6.0 版本中引入
  • 属性如 loading 和 fetchpriority 的默认值由 wp_get_loading_optimization_attributes() 函数确定
  • decoding 属性可设置为 'async'、'sync' 或 'auto',传递 false 或空字符串将省略该属性

📄 原文内容

Filters the HTML img element representing an image attachment.

Parameters

$htmlstring
HTML img element or empty string on failure.
$attachment_idint
Image attachment ID.
$sizestring|int[]
Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
$iconbool
Whether the image should be treated as an icon.
$attrstring[]
Array of attribute values for the image markup, keyed by attribute name.
See wp_get_attachment_image() .

More Arguments from wp_get_attachment_image( … $attr )

Attributes for the image markup.

  • src string
    Image attachment URL.
  • class string
    CSS class name or space-separated list of classes.
    Default attachment-$size_class size-$size_class, where $size_class is the image size being requested.
  • alt string
    Image description for the alt attribute.
  • srcset string
    The 'srcset' attribute value.
  • sizes string
    The 'sizes' attribute value.
  • loading string|false
    The 'loading' attribute value. Passing a value of false will result in the attribute being omitted for the image.
    Default determined by wp_get_loading_optimization_attributes().
  • decoding string
    The 'decoding' attribute value. Possible values are 'async' (default), 'sync', or 'auto'. Passing false or an empty string will result in the attribute being omitted.
  • fetchpriority string
    The 'fetchpriority' attribute value, whether high, low, or auto.
    Default determined by wp_get_loading_optimization_attributes().

Source

return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );

Changelog

Version Description
5.6.0 Introduced.