1

On the front-end, I'm using a query to show all attachments attached to the current post. However, on the compose page, there's no way for a user to see how many images are attached without opening up the media gallery.

I would like to add a meta box (or something) to display the current number of attached images – perhaps something like this: http://cl.ly/image/0a2q171v3y2u

Is this possible?

Thanks!

2
add_action( 'add_meta_boxes', 'attached_images_meta' );

function attached_images_meta() {
    $screens = array( 'post', 'page' ); //add more in here as you see fit
    foreach ($screens as $screen) {
        add_meta_box(
            'attached_images_meta_box', //this is the id of the box
            'Attached Images', //this is the title
            'attached_images_meta_box', //the callback
            $screen, //the post type
            'side' //the placement
        );
    }
}
function attached_images_meta_box($post){
    $args = array('post_type'=>'attachment','post_parent'=>$post->ID);
    $count = count(get_children($args));
    echo '<a href="#" class="button insert-media add_media" data-editor="content">'.$count.' Images</a>';
}

The above will link to the media uploader. I added the link because the screenshot you posted looked like a link. Feel free to remove the link like so:

echo $count.' Images';

Just pop this in your functions.php, custom plugin, etc.

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.