admin管理员组

文章数量:1332865

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?

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?

Share Improve this question edited Jul 4, 2020 at 22:03 Ivan Shatsky 8901 gold badge7 silver badges12 bronze badges asked Jul 4, 2020 at 16:22 Ian TurnerIan Turner 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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?

本文标签: uploadsLoading and Referencing Images