javascript - 响应式全宽 Wordpress 帖子图片
问题描述
我有一个 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;
}
推荐阅读
- typescript - 打字稿ORM设置默认实体值不起作用
- filter - RankX 在组的子集中
- c++ - 使用 Aws::LexRuntimeV2::LexRuntimeV2Client 的问题 - QT/C++ 中的 RecognizeText
- ansible - jinja2模板中相等测试的sameas条件
- c64 - Commodore C64 如何检测 PAL 或 NTSC
- python - Pytest - 将信息传递给夹具
- javascript - 将 Material-UI Drawer 组件定位在 Appbar 下
- javascript - 错误堆栈的输出不包含所有信息
- android - RxJava 中相互依赖的多个 Retrofit 请求
- react-native - 如何在 onPress 事件上打开抽屉导航器