WordPress > Post-specific images with uploader and add/remove tool
I'm looking to find a WordPress plugin that ads an upload widget to my post editor to allow me to associate images with posts.
The end resu开发者_如何学Clt is that I can add a bit of code to my theme's single.php file that allows me to load all of the images that reside in the folder matching the post id.
If no such plugin exists, I'm looking for some help on creating one.
Here's the way it would work..
Any images uploaded to a specific post, will go into a folder named after the post id. For example, if you are editing the post with id=420, the uploaded files will go into a folder named "420". There would also need to be some way to add or remove images from the folder without leaving the post editor (though FTP could be used for this as well).
To show post specific images, the single.php file checks to see if there is a folder under wp-content/uploads matching the post id. If it finds a match, it simply loads all the images it finds in the folder.
You can already attach images to a post without including them in the main content. You can do all of this in the main Edit Post window (the first icon next to "Upload/Insert" above the text editor. They are added with the post id as parent_id
parameter, so the get_children
function will give you all attached files.
This will store the images in the standard /wp-content/uploads/
folder, grouped by year and month (if you checked that option in the administration page). If you want to change the upload directory, you can use the upload_dir
filter (slightly outdated parameter list) and return a different upload directory, for example with the post ID. The upload_dir
filter is called from wp_upload_dir()
, which is called by wp_handle_upload()
, which is called by media_handle_upload()
, which is called from the media-upload.php
file.
If you want to extend this idea and check the upload dir for new files (uploaded via FTP for example), and attach these to the post, I suggest you look into the attachment functions.
精彩评论