最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

attachments - Upload image and assign it's id to post

programmeradmin8浏览0评论

At the moment i am working on database conversation script from other cms to Wordpress and faced one problem: I have a list of images, so i need to upload it to Wordpress, after it receive new link, change it with the old one in the post and insert this post. So i wondering if is possiple after wp_insert_post somehow assign image id's to the post which just been inserted.

P.S. And also is there a method that after upload return url of file?

At the moment i am working on database conversation script from other cms to Wordpress and faced one problem: I have a list of images, so i need to upload it to Wordpress, after it receive new link, change it with the old one in the post and insert this post. So i wondering if is possiple after wp_insert_post somehow assign image id's to the post which just been inserted.

P.S. And also is there a method that after upload return url of file?

Share Improve this question edited Jan 8, 2013 at 21:45 user1692333 asked Jan 8, 2013 at 21:25 user1692333user1692333 4472 gold badges9 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Wordpress attachments are saved in the wp_posts table with the post_type field set to 'attachment'. The post id for the post that the attachment is 'linked' to is in the post_parent field. Keep in mind that an attachment being 'linked to a post is not really a solid connection. It is very possible to have an attachment linked to a post but not actually embedded into that post, and equally possible for the same attachment to actually be embedded into a completely different post. (Lets just call it an open relationship.)

Conversions are difficult, but if you adding the post programatically and you have the post id for it, if you are creating the attachment using wp_insert_post(), you can specify post_parent in your post array:

'post_parent'    => [ <post ID> ] //Sets the parent of the new post.

See the media_handle_upload and media_sideload_image functions to upload images and attach them to a post. You can get the url of a specific size with the returned attachment ID and the wp_get_attachment_image_src function.

发布评论

评论列表(0)

  1. 暂无评论