I've created a new custom post type and I need the post thumbnails or featured images generated by the get_the_post_thumbnail() function to be set in percent, rather than pixels, as I'm using a fluid grid. Ideally I would only set the width (to 100%). Is there a way to do this? Potentially using add_image_size()?


have you tried,

if(has_post_thumbnail()) {                    
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
     echo '<img src="' . $image_src[0]  . '" width="100%"  />';
| improve this answer | |
  • Smart +1. Should be plugin ;) – kaiser Jun 27 '11 at 1:25

It's not possible to change the units of the default width and height output, but you can possibly override the output via css:

/* these are the classes that get added per default */

Then you can override it via something like body img.attachment-large { width: 50% !important; height: 50% !important; }

| improve this answer | |

I've solved that issue getting rid of all width and height attributes set by WP:

    //tidy up img tags. We don't want inline height and width added by WP.
    //we'd rather use media queries and fluid img.
        function remove_image_dim_attr($html) {
            $html=preg_replace( '/width=(["\'])(.*?)\1/', '', $html );
            $html=preg_replace( '/height=(["\'])(.*?)\1/', '', $html );
            return $html;
        add_filter( 'get_image_tag','remove_image_dim_attr' );
        add_filter( 'image_send_to_editor','remove_image_dim_attr' );
        add_filter( 'post_thumbnail_html','remove_image_dim_attr' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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