I’m trying to change the image width and height attribute of a single post thumbnails. Here’s the HTML tag output generate by WordPress.
<img width="1024" height="600" src="http://www.sample.com/wp-content/uploads/2015/02/image.jpg" alt="My site" width="100%" height="" />
I want this width="1024" height="600"
to become like this width="100%" height=""
so the image is already responsive in all different window screen without doing CSS
or JQUERY
codes.
My codes below is not working correctly. The output is another width="100%" height="" />
after the src
attribute instead of replacing the width="1024" height="600"
before the src
attribute.
The image tag output I expected is to be like this
<img width="100%" src="http://www.sample.com/wp-content/uploads/2015/02/image.jpg" alt="My site" />
PHP CODE :
$attr = array(
'class' => '',
'alt' => 'My site'
'width' => '100%',
'height' => ''
);
get_the_post_thumbnail( $post->ID, 'full', $attr )
I have another way to solve this. The codes below is my second option but I also want to know how this image / height attribute change dynamically.
$img_url= wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
<img width="100%" src="<?php echo $img_url[0]; ?>" alt="My site" />
Any help is very appreciated.
You can find thumbnail url & give css to that
Create custom image sizes when new images are uploaded:
In your template:
html