0

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?

1 Answer 1

0

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?

2
  • Thank you mozboz. I do use the ID but I get the (post) ID by querying the last ID used (just before I upload the image) and then add 1 to give the 'expected' ID of the post that the image will be attached to ('$result->PostNum). Just seems odd but maybe there is no other way. Having done a lot of Googling, I could not find a better way!
    – Ian Turner
    Jul 4, 2020 at 20:56
  • A eureka moment... seems blindingly obvious when you first see that the function returns the ID on success! A good example of using code that I didn't fully understand.
    – Ian Turner
    Jul 5, 2020 at 4:40

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.