有问题的代码→
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
中生成的输出
browser 是这样的→
<img width="1620" height="973" src="..../site04/wp-content/uploads/2018/01/audi_ileana.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="..../site04/wp-content/uploads/2018/01/audi_ileana.jpg 1620w, http://......./site04/wp-content/uploads/2018/01/audi_ileana-300x180.jpg 300w, ....../site04/wp-content/uploads/2018/01/audi_ileana-768x461.jpg 768w, http://....../site04/wp-content/uploads/2018/01/audi_ileana-1024x615.jpg 1024w" sizes="(max-width: 1620px) 100vw, 1620px">
预期结果→我希望该高度应输出为“自动”值。
<小时>
I tried this:
<div class="pimg">
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
</div>
及其css:
.pimg img {max-with:100%; height:auto;}
最合适的回答,由SO网友:Andrew 整理而成
您可以使用过滤器从图像中删除高度和宽度属性,如下所述CSS Tricks. 在主题中放置以下内容functions.php
文件
add_filter( \'post_thumbnail_html\', \'remove_width_attribute\', 10 );
add_filter( \'image_send_to_editor\', \'remove_width_attribute\', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( \'/(width|height)="\\d*"\\s/\', "", $html );
return $html;
}