I'm currently using @kovshenin's tutorial here on adding a thumbnail (a featured image) to my post type admin columns.
Would it perhaps be possible ( and how :) ) to retrieve the first image attached to a post first, if no featured image available? (and perhaps return a local hosted image if also no featured image available)
This is the code I currently have:
add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}
add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
$column_thumbnail = array(
'thumbnail' => ''
);
$column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
return $column;
}
add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
global $post;
switch ( $column ) {
case 'thumbnail':
echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );
break;
}
}
Edit:
As mentioned in the next comments, it took some time to figure things out :). In the end, and with the suggestions of @ifdion I came up with the next result:
add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}
add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
$column_thumbnail = array(
'thumbnail' => ''
);
$column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
return $column;
}
add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
global $post;
$args = array(
'numberposts' => 1,
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_type' => 'attachment',
);
$attachments = get_children( $args );
if ( has_post_thumbnail() ) {
echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );
} elseif ( $attachments ) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'edit-screen-thumbnail' );
echo '<img src="' . $image_attributes[0] . '">';
}
} else {
echo '<img src="' . plugins_url( '/img/edit-screen-thumbnail.png', __FILE__ ) . '">';
}
}
Another edit
Not entirely sure if this grabs the last image attached to a post, but anyways, this code will get it:
add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}
add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
$column_thumbnail = array(
'thumbnail' => ''
);
$column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
return $column;
}
add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
global $post;
$args = array(
'numberposts' => 1,
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_status' => null,
);
$attachments = get_posts( $args );
if ( has_post_thumbnail() ) {
echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );
} elseif ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'edit-screen-thumbnail' );
}
} else {
echo '<img src="' . plugins_url( '/img/edit-screen-thumbnail.png', __FILE__ ) . '">';
}
}