Can I "attach" images to a post without adding it to the post?
The reason behind this is so that I can manipulate it however I want using an API.
-
I think your question is a bit unclear. When you write add/attach do you mean you want to insert it into the post?– hakreNov 4, 2010 at 2:35
-
2I want to associate several photos to post but I don't want to add it to post (eg, not shown in post but it's linked to that post). Is that clear enough?– ariefbayuNov 4, 2010 at 2:38
-
IMHO, Your clarifying comment should be made as an edit to the original question. It really is needed to make the question clear, and its relevance understood.– SherylHohmanDec 2, 2020 at 16:54
3 Answers
There is a Plugin called Attachments http://wordpress.org/extend/plugins/attachments/ Maybe this is something you looking for.
-
1It seems this does not save attachments in the same way the "core" WordPress systems saves attachments? Very similar, but not the same? Nov 4, 2010 at 15:50
-
1@Jan: No but it sounds exactly what silent asked for. Its a nice way to attach files to a post from the complete media library. I know that the folks from wordpress-deutschland got a plugin that let you attach media files to more then 1 post. Nov 5, 2010 at 7:43
Yes, its definitely possible: I do this in one of my themes.
You simply add your images to the post as if you were going to insert it into the post, but then just click save all changes, and do not actually click the button "Insert Into Post".
Then, you can access that post's gallery images using something like:
$images = get_gallery_images();
where I've defined that function in functions.php:
// get all of the images attached to the current post
function get_gallery_images() {
global $post;
$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
$galleryimages = array();
if ($photos) {
foreach ($photos as $photo) {
// get the correct image html for the selected size
$galleryimages[] = wp_get_attachment_url($photo->ID);
}
}
return $galleryimages;
}
And then do whatever you'd like to those images in your template files. (In my case, I loop through the images and put them in a jQuery slider).
There are also plugins that you could use, but its always best to minimise plugins if you can help it.
-
1
but then just click save all changes, and do not actually click the button "Insert Into Post".
In current version of WP, you have to actually "Insert into Post" the image, but if you then immediately backspace to delete it, then click "Update" to Update the Post, the post looks exactly the same as before, but now the media page lists that page as "attached" to that image (or rather that image is attached to that page). Dec 2, 2020 at 16:47 -
There is also the "attach" function when looking at the images in the Media Library. Dec 4, 2020 at 16:58
Yes, you can.
If you upload the image using the media uploader on a post's edit screen, or use update_post() to set the post_parent
field of the attachment to the ID of the post you want to attach it to, it is associated with that post, whether or not it is actually inserted into the content of that post.
You can retrieve all images attached to a specific post by calling get_children()
(see the codex for examples).