get_image_tag_class
云策文档标注
概述
get_image_tag_class 是一个 WordPress 过滤器,用于修改附件图像标签的 CSS 类属性值。它允许开发者自定义图像标签的类名,常用于添加响应式或样式类。
关键要点
- 过滤器名称:get_image_tag_class
- 参数:$class(CSS 类名)、$id(附件 ID)、$align(对齐方式)、$size(图像尺寸)
- 应用场景:主要用于 get_image_tag() 函数生成的图像标签,不适用于 the_post_thumbnail()
- 版本:自 WordPress 2.6.0 引入
代码示例
function example_add_img_class( $class ) {
return $class . ' img-fluid';
}
add_filter( 'get_image_tag_class', 'example_add_img_class' );注意事项
- 此过滤器仅影响通过 WYSIWYG 编辑器插入的图像,不适用于 the_post_thumbnail() 函数
原文内容
Filters the value of the attachment’s image tag class attribute.
Parameters
$classstring-
CSS class name or space-separated list of classes.
$idint-
Attachment ID.
$alignstring-
Part of the class name for aligning the image.
$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).
Source
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
Skip to note 2 content
Emil
To add the .img-fluid class, for responsive images in Bootstrap 4:
function example_add_img_class( $class ) { return $class . ' img-fluid'; } add_filter( 'get_image_tag_class', 'example_add_img_class' );