簡體   English   中英

WordPress:在 img 標簽中回顯特色圖片

[英]WordPress: echo featured image in img tag

我正在嘗試在頁面模板中獲取特征圖像並將其打印在這樣的引導代碼塊中

    <div class="container">
            <?php
                $args=array('post_type' => 'partner');
                $query= new WP_Query($args);                               
                // Start the Loop.
                while ($query-> have_posts() ) : $query->the_post()?>                                    

         <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
           <img src="<?php  echo the_post_thumbnail();?>" class="img-responsive"> 
         </div>
      <?php                                
          endwhile;
       ?>

    </div>                             

這不是打印圖像有什么問題請幫忙

the_post_thumbnail()已經打印了縮略圖……這意味着您不應將echo與它結合使用。 它還打印整個<img />標簽......而不是源代碼。 它的第二個參數是一個屬性數組。

正確的用法如下所示:

<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive' ) ); ?>
</div>

以下是該標簽的工作原理:

<?php the_post_thumbnail( $size, $attr ); ?>

$size — 圖像大小(關鍵字或維度數組)

$attr — 屬性/值對數組。

在 Codex 中閱讀更多內容。

在此參考中,您也可以通過將圖像 url 放在 img 標簽的 href 中來實現。 所以你的完整代碼將是這樣的:

<div class="container">
<?php
                $args=array('post_type' => 'partner');
                $query= new WP_Query($args);                               
                // Start the Loop.
                while ($query-> have_posts() ) : $query->the_post()?>                                    

         <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
         <?php if (has_post_thumbnail( $post->ID ) ): ?>
        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
        <img src="<?php echo $image[0]; ?>" class="img-responsive"> 
        <?php endif; ?>

         </div>
      <?php                                
          endwhile;
       ?>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM