How to display the images attached to a post inside the admin?

Hello stackoverflow friends!

Hopefully somebody here can help me with this! I’ve looked all over the internet with no luck 🙁

Read More

What I want to do is somehow show the images attached to a post inside the WP admin. For example, the same way the post Featured Image shows. The only thing I found was how to display the number of attachments of a post in the admin columns but what I would like to see is thumbnails of all attached images inside each post in the post admin page. I think this will be very helpful since right now I cannot even tell which post has attachements.

I will like to accomplish this without using a plugin.

I have looked for days online with no luck. Any help will be greatly appreciated.

Thank you very much in advanced!

Related posts

Leave a Reply

2 comments

  1. The solution is to create a meta box and then put the thumbnails in there. This requires the add_meta_box() function and wp_get_thumb_attachment_url(). We also need to find all attached images of a certain post, which we’ll do using the answer here.

    Putting all this together, and assuming PHP version >= 5.3 so that we can use anonymous functions, it will look like this:

    add_action( 'add_meta_boxes', function() {
    add_meta_box( 'att_thumb_display', 'Attachmed images', function( $post ) {
        $args = array(
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'post_parent' => $post->ID
        );
    
        echo '<ul>';
        foreach( get_posts( $args ) as $image) {
            echo '<li><img src="' . wp_get_attachment_thumb_url( $image->ID ) . '" /></li>';
        }
        echo '</ul>';
    }, 'post' );
    });
    

    The argument of add_meta_box that I’ve set to “post” indicates on which post type this meta box will be available. If you want it available on pages, you have to set it to pages. Or if you want it to be available on a custom post type, you will also have to modify it accordingly.

    I hope this works. I haven’t tried it.

  2. try this :

       /* === Add Thumbnails to Posts/Pages List === */
    if ( !function_exists('o99_add_thumbs_column_2_list') && function_exists('add_theme_support') ) {
    
        //  // set your post types , here it is post and page...
        add_theme_support('post-thumbnails', array( 'post', 'page' ) );
    
        function o99_add_thumbs_column_2_list($cols) {
    
            $cols['thumbnail'] = __('Thumbnail');
    
            return $cols;
        }
    
        function o99_add_thumbs_2_column($column_name, $post_id) {
    
                $w = (int) 60;
                $h = (int) 60;
    
                if ( 'thumbnail' == $column_name ) {
                    // back comp x WP 2.9
                    $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
                    // from gal
                    $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
                    if ($thumbnail_id)
                        $thumb = wp_get_attachment_image( $thumbnail_id, array($w, $h), true );
                    elseif ($attachments) {
                        foreach ( $attachments as $attachment_id => $attachment ) {
                            $thumb = wp_get_attachment_image( $attachment_id, array($w, $h), true );
                        }
                    }
                        if ( isset($thumb) && $thumb ) {
                            echo $thumb;
                        } else {
                            echo __('None');
                        }
                }
        }
    
        // for posts
        add_filter( 'manage_posts_columns', 'o99_add_thumbs_column_2_list' );
        add_action( 'manage_posts_custom_column', 'o99_add_thumbs_2_column', 10, 2 );
    
        // for pages
        add_filter( 'manage_pages_columns', 'o99_add_thumbs_column_2_list' );
        add_action( 'manage_pages_custom_column', 'o99_add_thumbs_2_column', 10, 2 );
    }
    

    it will show you the featured image as preview in the manage post /page list ,
    If you want to show it inside the post itself – use @Calle suggestion:
    (modified here to work)

    add_action( 'add_meta_boxes', 'o99_add_attach_thumbs_meta_b' );
    
    function o99_add_attach_thumbs_meta_b (){
    
    add_meta_box ('att_thumb_display', 'Attached images','o99_render_attach_meta_b','post');
    
    }
    
    function o99_render_attach_meta_b( $post ) {
    $output = '';
    $args = array(
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'post_parent' => $post->ID
        );
        //
        // uncomment if you want ordered list
        //
        // $output .= '<ul>';
         $images = get_posts( $args );
        foreach(  $images as $image) {
        //$output .= '<li>';
            $output .= '<img src="' . wp_get_attachment_thumb_url( $image->ID ) . '" />';
            //$output .= '</li>';
        }
       // $output .= '</ul>';
      echo $output;
    }