php – How do I add attributes to the img with the_post_thumbnail WordPress function?

Question:

I need to add a width="100%" and a class="img-responsive" .

How do I add it to the img if I call it to the index like this?

<?php the_post_thumbnail(); ?>

Answer:

Do it like this:

<?php
  $thumb_id = get_post_thumbnail_id();
  $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
  echo "<img src='".$thumb_url[0]."' width='100%' class='img-responsive' />";
?>

get_post_thumbnail_id gets the id of the thumb.

wp_get_attachment_image_src gets the image url, passing the thumb id as parameter.

Scroll to Top