Luckily it’s easy to add custom columns to the Admin area in WordPress. If you want to show the post thumbnail/featured image in the admin area, use this code snippet:
if ( ! function_exists( 'wpsnippets_custom_columns_posts' ) ) {
/**
* Add custom columns to edit posts admin screen
*/
function wpsnippets_custom_columns_posts( $columns, $post_type ) {
// Init
$new_columns = array();
// Change 'post' to the slug of the post type you want to show the image for
if ( 'post' === $post_type ) {
// Place the checkbox ('cb') column as the first column
$new_columns[ 'cb' ] = '<input type="checkbox">';
// Add the custom column with a dashicon as column title
$new_columns[ 'featured_image' ] = '<span class="dashicons dashicons-format-image" style="display:block;margin:0 auto;"></span>';
}
// Add new columns to the start of the original columns
$columns = array_merge( $new_columns, $columns );
return $columns;
}
}
add_filter( 'manage_posts_columns', 'wpsnippets_custom_columns_posts', 10, 2 );
if ( ! function_exists( 'wpsnippets_column_content_posts' ) ) {
/**
* Output the content of custom columns
*/
function wpsnippets_column_content_posts( $column, $post_id ) {
if ( 'featured_image' === $column ) {
echo '<div class="thumbnail-wrapper" style="width:50px;height:50px;background-color:#d9d9d9;">';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'thumbnail', array( 'style' => 'width:50px;height:auto;' ) );
}
echo '</div>';
}
}
}
add_action( 'manage_posts_custom_column', 'wpsnippets_column_content_posts', 10, 2 );
if ( ! function_exists( 'wpsnippets_posts_columns_styling' ) ) {
/**
* Add styling to the column so it looks good
*/
function wpsnippets_posts_columns_styling() {
echo '<style>.column-featured_image{width:50px;}</style>';
}
}
add_action( 'admin_head', 'wpsnippets_posts_columns_styling' );
This code checks if the custom column is the “Custom Column” column and if the post has a thumbnail, and if so, displays the post’s featured image in the column.
Note: If you want to add the custom column to a custom post type, replace manage_posts_columns
and manage_posts_custom_column
with manage_{$post_type}_posts_columns
and manage_{$post_type}_posts_custom_column
, respectively, where $post_type
is the name of your custom post type.