Wordpress: Get description, caption, alt, file URL

2019-09-03 20:12发布

问题:

I am writing a shortcode for wordpress

[insert_image id="#NUM"]

Description of the code: The code looks up the image number #NUM and retrieve all the information such as: alt test, description, title, caption, file URL, etc. This information is used to format the image in a way that is similar to Wikipedia (caption on top, then the image, description in the bottom and then the credit to the photographer, all framed in a box with shadow :)).

How I tried to sort this out: Unfortunately online there is a lot of stuff on retrieving thumbnails properties, but little about images that are NOT attached to the post, as it is in this case. As I learned that the image is treated as a post in Wordpress, I tried to retrieve the post whose id is the id of the image, but Wordpress returned NULL. Tried other ways, but got some information on the picture like width and height, but not the information I was looking for. I even looked at the Wordpress code that displays the image properties "add an image"->"media library", but I was not able to find the answer (that code is too advanced for me).

Question: So, I wonder is there anyone that can help me retrieve all the proprieties associated with an image identified just by the id but NOT attached to the post?

回答1:

Try wp_prepare_attachment_for_js

I have yet to use it, but I know its supposed to grab all the information from an attachment including title, description, alt, caption, etc.