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.

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

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

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%"  />';
} 
share|improve this answer
    
Smart +1. Should be plugin ;) –  kaiser Jun 27 '11 at 1:25
    
Perfect! Thanks! –  daysrunaway Jul 1 '11 at 5:41
add comment

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

share|improve this answer
add comment

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' );
share|improve this answer
add comment

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.