2021年5月WordPress 5.7 ✔
默认值the_post_thumbnail()
将从WordPress输出所有必需的属性。
<?php the_post_thumbnail(); ?>
<!-- DOM -->
<img
src="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1.jpg"
class="attachment-post-thumbnail size-post-thumbnail wp-post-image"
alt=""
loading="lazy"
srcset="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1.jpg 1500w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-300x160.jpg 300w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-1024x546.jpg 1024w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-768x410.jpg 768w"
sizes="(max-width: 1500px) 100vw, 1500px"
width="1500"
height="800"
>
使用class
数组属性
使用
class
没有
$size
参数或
$size
像
thumbnail
将删除
srcset
属性。因为当你的尺寸只有
150 x 150
.
<?php the_post_thumbnail(array(\'class\' => \'classname\')); ?>
<?php the_post_thumbnail(\'thumbnail\' array(\'class\' => \'classname\')); ?>
<!-- DOM -->
<img
src="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-150x150.jpg"
class="classname wp-post-image"
alt=""
loading="lazy"
width="150"
height="150"
>
srcset
将提供
thumbnail
. 可用尺寸为
thumbnail
,
medium
,
large
,
full
. 可以在WordPress的仪表板中调整大小>;设置(>);媒体\'
thumbnail: 150px
medium: 300px
large: 1024px
full: Your original uploaded size
使用
medium
大小相同。
<?php the_post_thumbnail(\'medium\' array(\'class\' => \'classname\')); ?>
<!-- DOM -->
<img
src="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-300x160.jpg"
class="classname img-fluid wp-post-image"
alt=""
loading="lazy"
srcset="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-300x160.jpg 300w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-1024x546.jpg 1024w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-768x410.jpg 768w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1.jpg 1500w"
sizes="(max-width: 300px) 100vw, 300px"
width="300"
height="160"
>
使用函数
可以使用函数将类包含到帖子中(函数由@s\\u ha\\u dum提供)。Iam添加引导
img-fluid
在这里小心!完全阅读
// using function to add class to `the_post_thumbnail()`
function alter_attr_wpse_102158($attr) {
remove_filter(\'wp_get_attachment_image_attributes\',\'alter_attr_wpse_102158\');
$attr[\'class\'] .= \' img-fluid\';
return $attr;
}
add_filter(\'wp_get_attachment_image_attributes\',\'alter_attr_wpse_102158\');
注意引导类
img-fluid
添加到类属性中。
<?php the_post_thumbnail(); ?>
<!-- DOM -->
<img
src="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1.jpg"
class="attachment-post-thumbnail size-post-thumbnail img-fluid wp-post-image"
alt=""
loading="lazy"
srcset="http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1.jpg 1500w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-300x160.jpg 300w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-1024x546.jpg 1024w,
http://example.com/wp-content/uploads/2021/04/your-image-1500x800-1-768x410.jpg 768w"
sizes="(max-width: 1500px) 100vw, 1500px"
width="1500"
height="800"
>
功能class
使用函数时,在第2个post上消失class
只在第一个帖子上有效,在第二个帖子上消失了。直接在上使用该类the_post_thumbnail()
<?php the_post_thumbnail(\'full\' array(\'class\' => \'img-fluid\')); ?>
记住
srcset
属性对于
thumbnail
.