Create a gist now

Instantly share code, notes, and snippets.

Create 'product' post type thumbnail column
<?php
add_action( 'after_setup_theme', 'cor_after_setup_theme' );
/**
* lorem
*/
function cor_after_setup_theme() {
add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}
add_filter( 'manage_edit-product_columns', 'cor_add_thumbnail_column', 10, 1 );
/**
* Create 'product' post type thumbnail column.
*
* @param unknown_type $column
* @return number
*/
function cor_add_thumbnail_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_column_thumbnail', 10, 1 );
/**
* Get the column thumbnail.
*/
function cor_manage_column_thumbnail() {
global $post;
$attachments = get_children( array( 'numberposts' => 1, 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_type' => 'attachment' ) );
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__ ) . '">';
}
}
@paaljoachim

Hi

I tried your code and it is not working for me, as it pushes a thumbnail into multiple columns in the all posts screen.

I am looking into using a similar code as to what you posted above along with Konstantin Kovshenin's code on adding a thumbnail and wordcount to the all post screen. http://theme.fm/2011/07/hacking-the-wordpress-admin-mastering-custom-columns-758/

As it would be really great to have code to show the thumbnail in the all posts screen. If there is no featured image, then use the first image in the post, and if there is not image in the post use a default image.

I will very likely create a tutorial showing the code http://easywebdesigntutorials.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment