Is there a way to check if a gallery is linked to an attachment page or to an image file via jQuery or WP filter?

I'm trying to add a lightbox to gallery image that is linked to image file. But I can't do it since there's no way to tell the difference between image linked to attachment page or image file in the html codes generated by WordPress.

1 Answer 1


By default there is a difference in the HTML output for images inserted alone or via [gallery].

A direct image link will output an <a href=""> to an actual image mime type, like:
<a href="some-image.jpg">

An attachment <a href=""> outputs an attachment URI like: <a href="some-link?attachment_id=126" or ../attachment/2013/

As for a filter I don't think there is a way unless you parse the_content for the above matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.