简体   繁体   English

WordPress调整移动设备的图像大小

[英]WordPress resize images for mobile devices

In a WordPress post I have this HTML tag. 在WordPress的帖子中,我有这个HTML标签。

<img class="size-full wp-image-761" src="http://domain.com/wp-content/uploads/2016/09/couple.jpg"  width="650" height="433" />

Which when rendered produces 哪个渲染产生

<img class="size-full wp-image-761" src="http://domain.cloudfront.net/wp-content/uploads/2016/09/couple.jpg" width="650" height="433" srcset="http://domain.cloudfront.net/wp-content/uploads/2016/09/couple.jpg 650w, http://d14x51nv4ivcb0.cloudfront.net/wp-content/uploads/2016/09/couple-300x200.jpg 300w" sizes="(max-width: 650px) 100vw, 650px">

When I view the page in a browser on a PC everything is fine, but when I view the page on my phone is incorrect. 当我在PC上的浏览器中查看页面时一切都很好,但是当我查看手机上的页面时是不正确的。 The image is too big for the screen so I only see the left part of the image and I am unable to move the page to the right to see the rest of the image. 图像对于屏幕而言太大,所以我只看到图像的左侧部分,我无法将页面向右移动以查看图像的其余部分。

How do I handle the resizing of images when viewing on multiple devices? 在多台设备上查看时如何处理图像大小调整?

Try this: 尝试这个:

<img class="size-full wp-image-761" src="http://domain.com/wp-content/uploads/2016/09/couple.jpg"  width="100%"/>

I just changed the width attribute to 100% . 我刚刚将width属性更改为100%

Add this code in functions.php It will remove width height attribute of image when you add image to post content. 在functions.php中添加此代码当您将图像添加到发布内容时,它将删除图像的宽度高度属性。

add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10, 3 );
function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

Now add following css in your style.css file 现在在style.css文件中添加以下css

img{
     max-width:100%;
     height:auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM