I'm just looking for a relatively simple way to modify the default output when a standard post image with a caption is linked somewhere (generally to another page), so that the caption is included in the link. It currently resides outside of the link tag so that it doesn't link like the image does.

I'll eventually be moving the caption up so it overlays the image. It's just awkward that the image will link properly to a separate page, but the caption (which will be the title of the page that the link points to) will not.

The default output is currently:

<div id="attachment_xxx" style="width: 480px" class="wp-caption alignnone">
<a href="/about/history-mission/">
<img class="size-medium wp-image-1679" src="https://domain.com/wp-content/uploads/sample-image.jpg" alt="" width="470" height="313" srcset="[multiple image sizes]" sizes="[proper sizes]">
<p id="caption-attachment-1679" class="wp-caption-text">History & Mission</p>
  • Any way to get that p.wp-caption-text to slide into the 'a' tag with the image???

If there is no link added to a photo, it should just behave normally. I also thought about using a gallery, but didn't see a way to make each image a unique link to other pages.

NOTE: For now I'm using the classic editor (w/ TinyMCE Advanced). Not sure if I'll switch to Gutenberg yet, as that would open a TON of possibilities up to the client that I would have to account for & style appropriately. Because, you know, clients...


  • I'm wondering if this is a challenge due to WordPress not wanting to wrap links around block elements?!? I've hacked a workaround by not using captions but just typing the caption text manually next to an image (no breaks), then selecting text & image and making the link. I can then add a span around the text to help style easier. But this is much more challenging to maintain than using a caption like the original question, and I worry our client would fail here. Anyone?? – Kenny J May 10 '19 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.