public
Last active

Create 'product' post type thumbnail column

  • Download Gist
gistfile1.phtml
HTML+PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<?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__ ) . '">';
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.