How do I attach images to a post without inserting them?

I’m developing a theme with an built-in gallery outside of the post content. So what I want to do is to allow editors to attach images to a page/post without inserting them.

Kind of like the featured image, but with any number of images.
Just a nice UI for uploading, and leave it to the theme to render it.

Read More

I looked at some plugins, especially NextGen Gallery, but so far they all work by inserting a shortcode in the post body.

Related posts

1 comment

  1. You don’t have to insert image into post after attaching it to this post.

    When you edit post, click on ‘Add Media’ button. Upload your image and when it’s uploaded just close the ‘Insert Media’ dialog (use ‘x’ button in top right corner and not ‘Insert into post’ button).

    You can then get (and show) them with this code:

    <?php
        $images = new WP_Query(array('post_type' => 'attachment', 'post_mime_type' =>'image', 'posts_per_page' => -1));
        while ($images->have_posts()) : $images->the_post();?>
            <a href="<?php echo wp_get_attachment_url($post->ID); ?>">
                <img width="100" height="100" src="<?php echo wp_get_attachment_thumb_url( $post->ID);?>" />
            </a>
    <?php endwhile; ?>
    

    You could also use get_children function to get these attachments (see below).

    $attachments = get_children(array(
        'post_parent' => $post->ID,
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
    ));
    
    foreach ($attachments as $id=>$att ) { ... }
    

Comments are closed.