Add a Class to ‘the_post_thumbnail’ a Featured Image

When adding a featured image to a template, you may want to add a class to the image tag that is being generated. This extra class will allow you to use CSS to format it better.

Code Snippet:

if ( has_post_thumbnail() ) {
the_post_thumbnail('medium', array('class' => 'alignleft'));

Comments  Leave a Reply

  1. this doesn’t work, this REPLACES the set of classes by your own

  2. Hi How can i add a div as parent element to the image? thanks

    1. You won’t be able to with the code above. However you could do the following

      if ( has_post_thumbnail() ) {
      echo '';

      1. Where should I paste the code sire

  3. Freelance Web Designer May 20, 2016 at 7:53 am

    Thank you, it’s working 🙂

  4. saved my day!!! Thank you

  5. Thank you 😉 I was looking for it 😉

    Happy new year!

  6. Thx a lot!! This is what I was looking for!!

  7. Thx a lot!! This is what I was lokking for!!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!