1

I've searched awhile for a solution on how to get the attached image in the post's editor to display within the dashboard but I am running into an issue. I've seen how to do this and can do this with the thumbnail:

function foo_attached_image( $post_ID ) {
    $post_thumbnail_id = get_post_thumbnail_id( $post_ID );
    if ( $post_thumbnail_id ) {
        $post_thumbnail_img = wp_get_attachment_image_src( $post_thumbnail_id );
        return $post_thumbnail_img[0];
    }
}

but the thumbnail is already being used and has a different size then I need. When I try to modify my function that includes manage posts custom column to only pull the image I cant seem to get it to work with wp_get_attachment_url():

function attached_image_content( $column_name, $post_ID ) {
    if ( $column_name == 'foobar' ) {
        $post_attached_image = wp_get_attachment_url( $post_ID );
        if ( $post_attached_image ) {
            echo '<img style="width:100%;" src="' . $post_attached_image . '" />';
        }
    }
}
add_action( 'manage_cpt_posts_custom_column', 'attached_image_content', 10, 2 );

and I've tried with wp_get_attachment_image_src() as $post_attached_image = wp_get_attachment_image_src( $post_ID, 'medium' ); with still no go. If I use get_intermediate_image_sizes() I can get the array of everything attached but for some reason I cannot seem to get whatever image was loaded in the editor to display. What is the appropriate way to render the first uploaded image excluding the thumbnail so it will be displayed in the dashboard?

1 Answer 1

0

You need the attachment ID - which is the same as the "thumbnail ID" - not the post ID. It's confusing. Maybe by WP 5.0 they'll update the nomenclature!

So, I think this'll work:

function attached_image_content( $column_name, $post_ID ) {
    if ( $column_name == 'foobar' ) {

        //Change the image size from default 'thumbnail' here
        $post_attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'medium' );
        if ( $post_attached_image ) {
            //as you probably know, the url is the first array value
            echo '<img style="width:100%;" src="' . $post_attached_image[0] . '" />';
        }
    }
}
add_action( 'manage_cpt_posts_custom_column', 'attached_image_content', 10, 2 );
2
  • thanks for the answer but I a'm looking for anything BUT the thumbnail that is in the post, so get_post_thumbnail_id() wouldn't be correct.
    – user9447
    Jul 22, 2016 at 1:41
  • That's what I meant about the nomenclature being confusing... get_post_thumbnail_id actually gets you the attachment ID that you need for wp_get_attachment_image_src, which defaults to thumbnail, but will take any registered image size ('medium' in the above example, as per your code).
    – CK MacLeod
    Jul 22, 2016 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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