I have created a custom taxonomy, this taxonomy is dedicated to image attachments, when I upload my images $post->post_content returns a string like:

'[caption id="attachment_98" align="alignnone" width="300"]
  <a href="http://....jpg">
     <img class="size-medium" title="title" src="http://.../wp-content/uploads/2012/10/name.jpg" alt="cardinal2" width="300" height="225" />

By calling the_content() this string becomes an HTML markup, at codex I couldn't find a function that allows me to get the source of the image and caption/description/title of it in a dynamic way, is there a way to output the content without using the_content() function? Should I split the result?


Filter 'img_caption_shortcode'. You get three arguments: an empty string, the attributes (including the attachment id), and the caption content. If you return anything but an empty string WordPress will print your return value instead of its own code.

See wp-includes/media.php for details.

Another option: hijack the caption handlers and create your own output or change the output WordPress builds before it is sent back to the content.

  • Thank-you very much for your answer, does this work when I have more than 1 image in my post? is there any other way that I can return title and description of the image too? I am about to create a gallery and I want to json_encode the results.
    – undefined
    Oct 10 '12 at 4:02
  • See my update, especially the linked answer.
    – fuxia
    Oct 10 '12 at 4:07

Your Answer

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

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