Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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

1 Answer 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 ) { ... }
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.