我有一个 Wordpress 网站,我的帖子中的图片会强制我网站的宽度,当它的移动尺寸时会破坏它。寻找基于容器大小使 Wordpress 帖子图像全宽(自动高度保持比例)的代码。
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="container">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php endif; ?>
编辑
function theme_content_image_sizes_attr( $sizes, $size ) {
$width = $size[0];
if ( 740 <= $width ) {
$sizes = '(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px';
}
return $sizes;
}
add_filter( 'wp_calculate_image_sizes', 'theme_content_image_sizes_attr', 10, 2 );
最佳答案
只是一个建议,也许你可以使用特色图片作为背景,这样你就可以控制宽度和高度。
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="header-wrap" style="background-image: url('<?php echo $backgroundImg[0]; ?>')">
</div>
.header-wrap {
background-repeat: no-repeat;
background-size: cover;
padding: 80px 0;
}
关于javascript - 响应式全宽 Wordpress 帖子图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211146/