How to add featured image in post listing page in WordPress admin?

10 Nov

How to add featured image in post listing page in WordPress admin?

If you want to add featured image in post listing page in WordPress admin dashboard, this post is for you. Here, I am going to show you how you can add featured image in post listing page.

Just use the code below in your functions.php or in the plugin code to make the featured image appear in listing page. One more tweak I did was, I just wanted my featured image to appear in listing page of my custom post type called “partners“.

add_action( 'current_screen', 'thisScreen' );

function thisScreen() {

    $currentScreen = get_current_screen();

    if( $currentScreen->post_type === "partners" ) {

        add_filter('manage_posts_columns', 'add_thumbnail_column', 5);

    }
    
}

function add_thumbnail_column($columns){
  $columns['new_post_thumb'] = __('Featured Image');
  return $columns;
}
 
add_action('manage_posts_custom_column', 'display_thumbnail_column', 5, 2);
 
function display_thumbnail_column($column_name, $post_id){
  switch($column_name){
    case 'new_post_thumb':
      $post_thumbnail_id = get_post_thumbnail_id($post_id);
      if ($post_thumbnail_id) {
        $post_thumbnail_img = wp_get_attachment_image_src( $post_thumbnail_id, 'thumbnail' );
        echo '<img style="max-width: 180px; padding: 10px 30px; vertical-align: middle;" src="' . $post_thumbnail_img[0] . '" />';
      }
      break;
  }
}</pre>
<pre>

Here I used two actions current_screen and manage_posts_custom_column. You can see details about them from codex. If your theme does not support featured image, here is a nice post showing how you can enable featured image in your theme.

Bydr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.