13

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.

3
  • I think your question is a bit unclear. When you write add/attach do you mean you want to insert it into the post? – hakre Nov 4 '10 at 2:35
  • 1
    I 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? – ariefbayu Nov 4 '10 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. – SherylHohman Dec 2 '20 at 16:54
5

There is a Plugin called Attachments http://wordpress.org/extend/plugins/attachments/ Maybe this is something you looking for.

2
  • 1
    It seems this does not save attachments in the same way the "core" WordPress systems saves attachments? Very similar, but not the same? – Jan Fabry Nov 4 '10 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. – Horttcore Nov 5 '10 at 7:43
8

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.

2
  • 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). – SherylHohman Dec 2 '20 at 16:47
  • There is also the "attach" function when looking at the images in the Media Library. – gillespieza Dec 4 '20 at 16:58
7

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).

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.