10

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 } ?>
4

3 回答 3

17
<?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 } ?>
于 2013-09-20T03:31:55.667 回答
6
<? if( has_post_thumbnail( $post_id ) ): ?>
    <div class="post-image">
        <img title="image title" alt="thumb image" class="wp-post-image" 
             src="<?=wp_get_attachment_url( get_post_thumbnail_id() ); ?>" style="width:100%; height:auto;">
    </div>
<? endif; ?>
于 2014-02-03T01:05:57.150 回答
2

You could also just use

<img src="<?php echo get_the_post_thumbnail_url (); ?>" style="width:100%; height:auto;">
于 2018-06-18T14:40:29.230 回答