admin管理员组

文章数量:1122832

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?

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?

Share Improve this question asked Jul 21, 2016 at 20:07 user9447user9447 1,7927 gold badges30 silver badges55 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: attachmentsGet post attached image to display in admin dashboard