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?
1 Answer
Reset to default 0You 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
版权声明:本文标题:attachments - Get post attached image to display in admin dashboard 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296632a1929839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论