Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I remove the width and height attributes from the post_thumbnail when inserting with <?php the_post_thumbnail(); ?>?

<img width="800" height="533" src="http://domain.com/wp-content/uploads/2011/02/image.jpg" class="attachment-post-thumbnail wp-post-image" />
share|improve this question
    
look for thumbnail.php file, not sure but usually that files contain the hard coding –  ntechi Jul 7 '11 at 14:54
    
In case you were wondering, I want to be able to modify widths and height with CSS - more specifically set a max-width and let the height set itself rather than default to the hard coded one. –  Carson Jul 7 '11 at 14:54
    
use firebug to get the exact class or ID, or give me your URL –  ntechi Jul 7 '11 at 14:56
1  
Changing anything in a file outside the theme (for example post-thumbnail-template.php) would be a bad idea as any WordPress update would overwrite it. –  Carson Jul 7 '11 at 15:10
    
What makes you think you need to remove the attributes to do what you want? –  t31os Jul 7 '11 at 17:27

2 Answers 2

up vote 12 down vote accepted

Related: Filter to remove image dimension attributes?

There's a filter on post_thumbnail_html which receives as its argument the full html element representing the post thumbnail image before it's echoed to the page. You can filter out the dimensions with a bit of regex:

add_filter( 'post_thumbnail_html', '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;
}
share|improve this answer
    
Not a bad solution at all. I'll give it a shot! Thanks. –  Carson Aug 2 '11 at 13:11
    
Works well - thanks. –  Adam Mar 20 '13 at 8:28
    
Simple and working solution. (vote up) –  CroiOS Jan 11 at 23:06
    
It does not work for me. add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 ); function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) { if ( wp_is_mobile() ) { $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html ); } return $html; } –  dlopezgonzalez Sep 11 at 10:20
    
This solution worked great! –  Randy Greencorn Nov 11 at 17:50

you could just grab the url of the thumb and put it in an img tag yourself:

<?php
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'your_thumb_handle' );
?>
<img src="<?php echo $thumbnail['0']; ?>" />
share|improve this answer
    
That's the only solution I've come across too, was just hoping there was a way to do it without a workaround. –  Carson Jul 7 '11 at 15:09
    
But you also need to query for the alt and title attributes –  MZAweb Apr 6 '12 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.