wordpress 将 the_post_thumbnail 宽度设置为 100%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18906199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:21:38  来源:igfitidea点击:

Setting the_post_thumbnail width to 100%

wordpress

提问by Brian

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

如何设置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 } ?>

回答by Subharanjan

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

回答by user3264236

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

回答by Leon Hoffmann

You could also just use

你也可以使用

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