javascript - 响应式全宽 Wordpress 帖子图片

标签 javascript css 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;
}

关于javascript - 响应式全宽 Wordpress 帖子图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211146/

相关文章:

jquery - 我怎样才能让这个插件不超过屏幕?

javascript - 从对象数组中搜索对象数组 - Javascript

html - list-style-image 没有出现在与列表项相同的级别?

wordpress - 显示描述而不是摘录,限制字数并添加阅读更多

css - wordpress qtranslate语言切换安装

css - 我在哪里可以在 CSS 中使用 + 和 >?

javascript - AngularJs - ngRepeat 删除 View 中的特定行

javascript - 计算字符串中重复的字母

javascript - 循环遍历json对象并将其放入html表中

javascript - 带有图像的div下的巨大空白