set_post_thumbnail_size in percent, not pixels?

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()?

Related posts

Leave a Reply

3 comments

  1. 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%"  />';
    } 
    
  2. 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 */
    img.wp-post-image
    img.attachment-thumbnail
    img.attachment-medium
    img.attachment-large
    img.attachment-full
    

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

  3. 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' );