0

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.

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

  • 2
    You can do this natively > media > upload and there is a link dedicated to this > attach media. – JMau Jun 20 '13 at 16:04
4

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 ) { ... }
  • How can the theme get attached but not inserted images from a post? – Pepijn Jul 11 '13 at 14:08
  • I've added some code samples to my answer - it should be clear right now. – Krzysiek Dróżdż Jul 11 '13 at 15:59

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.