Home / css / Add a Class to ‘the_post_thumbnail’ a Featured Image
  • How to Add a Class to ‘the_post_thumbnail’ a Featured Image

    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.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    the_post, has_post_thumbnail,
  • Tobi

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

  • Tobi

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

  • aleksiejs

    Thank you 😉 I was looking for it 😉

    Happy new year!

  • saved my day!!! Thank you

  • Thank you, it’s working :)

  • Chaz Cash

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

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

      if ( has_post_thumbnail() ) {
      echo '';
      }

      • Sajeed Musa

        Where should I paste the code sire

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