0

My client has decided he wants to add inside the article body, for each image, the caption below the img tag.

I know WordPress has this shortcode built in, but since there are more than 300 articles, it would take a lot of time.

[caption id="attachment_47" align="alignnone" width="300"] test test[/caption]

To recap, for each tag inside my article body, I want to return that tag and after that a paragraph tag which contains the caption of the image.

I found the wp_get_attachment_image() method but I can't make the connections when trying to get the article body and filtering those images in order to add the paragraph tag after each image.

Thanks a lot in advance!

1
  • Is adding a shortcode an option? We can replace all img tags with a shortcode if the structure allows us, we can do a search and replace database operation for all of them. The other solution I have for you is a function that searches through the content of each post, finds each img tag, gets its id, retrieves the caption associated with that image, and then appends a p tag with the caption below the img tag. But it involves manipulating HTML via DOMDocument, and it can be weird sometimes.
    – alex
    May 25, 2023 at 13:50

1 Answer 1

0

I'm not 100% sure if I understand your question 100%, but WordPress, by default, displays the caption if it's filled on the image. Then it displays it just below the image once the image is inserted.

Here is an example where I just filled it out on one image 👇 enter image description here

1
  • In my article body i'm using the text editor, so no gutenberg or other builders. I only have the img tag inside, not the caption shortcode WordPress provides. May 25, 2023 at 13:12

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.