sinanisler logo

add thumbnail column to the posts listing admin table

This code adds a “Thumbnail” column to the WordPress admin post listing page and displays the featured image for each post in that column.



// Add the new column to the post table post_thumbnail
function add_thumbnail_column( $columns ) {
  $columns['post_thumbnail'] = __('Thumbnail');
  return $columns;
}

// Hook the manage posts custom column action
add_filter( 'manage_posts_columns' , 'add_thumbnail_column' );

// Add the new column to the post types table
function add_thumbnail_column_to_post_types( $columns ) {
  $columns['post_thumbnail'] = __('Thumbnail');
  return $columns;
}

// Hook the manage posts custom column action for the custom post types
add_filter( 'manage_{post-type}_posts_columns' , 'add_thumbnail_column_to_post_types' );

// Add the data to the custom columns for the post
function display_thumbnail_column( $column, $post_id ) {
  if ( 'post_thumbnail' === $column ) {
    $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 src="' . esc_url( $post_thumbnail_img[0] ) . '" width="80" />';
    } else {
      echo __('No Thumbnail');
    }
  }
}

// Hook the manage posts custom column action to show thumbnail
add_action( 'manage_posts_custom_column' , 'display_thumbnail_column', 10, 2 );

// Add the data to the custom columns for the post types
function display_thumbnail_column_for_post_types( $column, $post_id ) {
  if ( 'post_thumbnail' === $column ) {
    $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 src="' . esc_url( $post_thumbnail_img[0] ) . '" width="80" />';
    } else {
      echo __('No Thumbnail');
    }
  }
}

// Hook the manage posts custom column action to show thumbnail for the custom post types
add_action( 'manage_{post-type}_posts_custom_column' , 'display_thumbnail_column_for_post_types', 10, 2 );


Leave the first comment