将the_post_thumbnail宽度设置为100%

时间:2013-09-19 22:50:31

标签: wordpress

如何设置the_post_thumbnail以使其不使用数组的大小,而是可以设置100%宽度和自动高度:

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

3 个答案:

答案 0 :(得分:16)

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

答案 1 :(得分:5)

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

答案 2 :(得分:1)

您也可以使用

<img src="<?php echo get_the_post_thumbnail_url (); ?>" style="width:100%; height:auto;">