Creating a custom class for featured images
-
I followed this tutorial https://www.wpbeginner.com/wp-tutorials/how-to-add-additional-fields-to-the-wordpress-media-uploader/ and was able to create the field for the image custom class. Now I am just having trouble adding the class name to the image. The tutorial said you can get the field value using the following code.
echo get_post_meta($post->ID, 'my-varible-name', true);
unfortunalry I am not sure where to add it so that it shows up as part of the class. I tried adding it in the media.php file under
function _wp_post_thumbnail_class_filter( $attr ) { $attr['class'] .= ' wp-post-image'; return $attr; }
I inserted it as
function _wp_post_thumbnail_class_filter( $attr ) { $attr['class'] .= get_post_meta($post->ID, 'my-varible-name', true); return $attr; }
This isn’t the right way to do it I know but I am not really sure what hook to use as that I am new to WordPress. My goal is for the featured image HTML to look like this
<img width="150" height="150" src="https://www.mysite/image.jpg" class="wp-post-image myVariable" alt="image">
I hope I have explained what I am trying to do correctly and any help would be greatly appreciated.
- The topic ‘Creating a custom class for featured images’ is closed to new replies.