Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the_post_thumbnail width to 100%

Tags:

wordpress

How to set the_post_thumbnail so that it doesn't use an array for its size, but instead can be set with a 100% width and auto height:

<?php $ht_featured_img = get_option('ht_featured_img'); 
if ($ht_featured_img == "true") { ?>
    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { /* if post has a thumbnail */ ?>
        <div class="post-image">
            <?php the_post_thumbnail( array(1215,9999) ); ?>
        </div><!--post-image-->
    <?php } ?>
<?php } ?>
like image 682
Brian Avatar asked Sep 19 '13 22:09

Brian


2 Answers

<?php $ht_featured_img = get_option('ht_featured_img'); if ($ht_featured_img == "true") { ?>
    <?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) { 
        $post_thumbnail_id = get_post_thumbnail_id();
        $post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
        ?>
        <div class="post-image">
            <img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto;">
        </div>
    <?php } ?>
<?php } ?>
like image 52
Subharanjan Avatar answered Sep 24 '22 07:09

Subharanjan


You could also just use

<img src="<?php echo get_the_post_thumbnail_url (); ?>" style="width:100%; height:auto;">
like image 20
Leon Avatar answered Sep 24 '22 07:09

Leon