How to Add custom attribute to image in WordPress

Prem Tiwari - - WordPress, WordPress Beginners, WordPress Snippets

In today’s tutorial, I will explain “How to Add a custom attribute to the image in WordPress“. This is very useful for all WordPress developers. Sometimes while you are developing a WordPress based website, then you need to add a custom attribute like class, alt, title, id, and etc to an image using the_post_thumbnail() function.


If you have just started with WordPress, then check out:

Below function will add the custom attributes in your image field. In the below example, I have added ‘class’=>’img-responsive’ extra attribute using the_post_thumbnail function. You can use the below function your theme and plugin files where it required.

Prem Tiwari

Prem Tiwari is the founder of and also a professional developer and a blogger by hobby.

Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.