4

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

3 Answers 3

11

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%"  />';
} 
1
  • Smart +1. Should be plugin ;)
    – kaiser
    Jun 27, 2011 at 1:25
0

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; }

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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