X

How To Add a Class to the Featured Image (Post Thumbnail)

Snippets by IsItWP

Ever wanted to add a class to the Featured Image (a.k.a. Post Thumbnail)? This snippet will add an extra class to the post thumbnail in a theme template.

Instructions

Add this snippet inside the loop of a WordPress theme template.

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our WPtouch Review.

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!