Search code examples
phpwordpresstwitter-bootstrapcustom-post-type

WordPress: echo featured image in img tag


i am trying to fetch feature image in page template and print it in bootstrap code block like this

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

this is not printing image whats wrong with it please help


Solution

  • the_post_thumbnail() already prints the thumbnail...which means you shouldn't use echo in combination with it. It also prints the entire <img /> tag...not the source. Its second parameter is an array of attributes.

    Proper usage would look like the following:

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

    Here's how that tag works:

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

    $size — Image size (keyword or array of dimensions)

    $attr — Array of attribute/value pairs.

    Read more in the Codex.