I’m slowly writing a page to manage an asset database; effectively, writing a very basic plugin. I want to import photos and found some excellent examples of how to do this and, made it work! My problem then was to display the images in the page. Although I know the uploaded image filename, there seems to be no way to use it. The image will be put into the current ‘date’ folder and could well be renamed on the way. I’ve ended up querying the last post ID just before I import the image and adding 1 to it to reference the image. With the post ID I can then use wp_get_attachment_image_src to get a url for the (resized) image:
wp_get_attachment_image_src( $result->PostNum, 'medium_large' );
It works but it is very clunky. Is there a better way of doing this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Could be the piece you’re missing here is that after you upload photos if you want to get to that item again to do anything with it, including get its URL, the thing you need to keep is not the filename but the ID that you get back.
You can then call e.g. wp_get_attachment_image_src directly with that ID to get the URL for that image.
If you don’t have a way to store that ID in a useful place and recall it when you need it, you may need to resort to some other way to figure it out, such as what you’re doing.
Does that help?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0