How do I add attributes in img with the_post_thumbnail Wordpress function?

Asked

Viewed 136 times

4

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

How do I add in img if I call it in index this way?

<?php the_post_thumbnail(); ?>
  • 2

    Try this way <?php the_post_thumbnail( 'thumbnail', array( 'class' => 'alignleft' ) ); ?>

1 answer

4

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 takes the id of the Thumb.

wp_get_attachment_image_src takes the image url, passing the Thumb id as parameter.

  • 1

    Very good, helped me too!

  • Opa worked, thanks man! and will help in future projects!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.