If you always add a post featured image to your article it could be very handy to see if a post has an associated featured image and which image it is.

With a few lines of code to be added to your theme functions.php or to a PHP file placed in the must-use plugins (wp-content/mu-plugins), you can add this nice feature.

Here is the code:

add_filter('manage_posts_columns', function ($columns) {
  $columns['image'] = 'Image';
  return $columns;
});

add_action('manage_posts_custom_column', function ($column_name, $post_id) {
  if ('image' === $column_name) {
    echo get_the_post_thumbnail($post_id, 'thumbnail', ['style'=>'width: 100px; height: auto']);
  }
}, 10, 2);

The first part adds a column to the standard WP post list and the second part fills that column with the post image thumbnail. That’s all.

Possibly you can choose to use another resized version, like the medium one which is usually not cropped, or enrich the code by adding a link to the image pointing to the original version.

Here is the final result:

If you need to add the thumbnail to the page list, the hook names should be changed: manage_posts_columns becomes manage_page_posts_columns and manage_posts_custom_column becomes manage_page_posts_custom_column.

Similar Posts

Leave a Reply