I’m using post thumbnails to link to a page.
Is it possible to add a class name to the post thumbnail image.
<li><a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail(); ?></a></li>
I’m using post thumbnails to link to a page.
Is it possible to add a class name to the post thumbnail image.
<li><a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail(); ?></a></li>
Comments are closed.
Yep – you can pass the class you want to use to
the_post_thumbnail()
as part of the attributes argument, for example<?php the_post_thumbnail('thumbnail', array('class' => 'your-class-name')); ?>
Ref: http://codex.wordpress.org/Function_Reference/the_post_thumbnail#Styling_Post_Thumbnails
You can filter those classes.
Add the filter just before you call
the_post_thumbnail
. The filter will remove itself automatically.It is a bit of trek to get there but
the_post_thumbnail
usesget_the_post_thumbnail
which useswp_get_attachment_image
which applies that filter.For most images in my websites I add a figure element around the images like below. That way I keep everything intact and still get to call the element with a class in the CSS.
May 2021
Tested and Working on WordPress 5.7 â
Default
Default
the_post_thumbnail()
will output all the required attributes from WordPress.Using
class
array attrib.Using the
class
without$size
parameter or the$size
asthumbnail
will remove thesrcset
attribute completely. Because why do your need responsive here when you size is only150 x 150
.srcset
will be available other thanthumbnail
. The available sizes arethumbnail
,medium
,large
,full
. Sizes can be adjusted in your WordPress ‘Dashboard > Settings > Media’Using the
medium
as size.Using the function
You can use function to include the class to the posts (function provided by @s_ha_dum). Iam adding bootstrap
img-fluid
here. Watchout! Read completelyNotice the bootstrap class
img-fluid
is added to the class attribute.function
class
disappeared on the 2nd postWhen using function, the
class
works only on the first post and it disappeared on the second one.Use the class directly on the
the_post_thumbnail()
Remember the
srcset
attribute is useless for thethumbnail
.