How to add an HTML class to all image attachments on posts in WordPress — except homepage

Sorry for the length, my problem is very simple, but specific.

I’m building a wordpress site that automatically imports image galleries from a Tumblr account, then styles and displays them. The backend is finished, and all that I need now is have all images in posts be displayed as thumbnail sized links. They are not behaving well with the importer (I’ve tried 6), so I have to edit the HTML display (rather than the images as they are uploaded).

I have decided to do this by applying class="attachment-thumbnail size-thumbnail" on all images on the site (in HTML). I have tested this and they will open the proper gallery in a lightbox as necessary so long as they belong to those two classes.

I have a two part problem.

  • I do not know PHP past basic syntax, and while I can intuit most of what is happening I don’t trust myself to edit WordPress source code without understanding exactly what I am breaking.
  • From other answers I’ve learned that I should most likely edit the wp_insert_attachment() function in post.php under wp_includes/. I have found the function, but don’t really know where to go from there, as it does not appear to be specific to image attachments. I don’t want to throw any errors by assinging image classes to non-image attachments. How do I add those two classes (attachment-thumbnail and size-thumbnail) to all post images (and only images)?

  • There is a single exception to this rule. I want a large image on the homepage, and have it not be a link.
  • It seems like the way to handle this would be to allow page attachments to be handled normally, while attachments that are both images and attached to posts should trigger this:

    if [attachment is an image AND is on a post, not a page]:


    tl; dnr: ^^ that’s all I need to happen, in PHP, in the right file, in WordPress ^^


    Read more here: How to add an HTML class to all image attachments on posts in WordPress — except homepage

    Leave a Reply

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