arie_cristianD

add featured image fields on post list

Aug 13th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. // Add Featured Image column before Title in the Posts list
  2. add_filter(
  3.     'manage_edit-post_columns',
  4.     function ( $columns ) {
  5.         $new = array();
  6.         foreach ( $columns as $key => $label ) {
  7.             if ( 'title' === $key ) {
  8.                 $new['featured_image'] = __( 'Featured Image', 'jnews' );
  9.             }
  10.             $new[ $key ] = $label;
  11.         }
  12.         return $new;
  13.     },
  14.     20
  15. );
  16.  
  17. // Output Featured Image in the column
  18. add_action(
  19.     'manage_post_posts_custom_column',
  20.     function ( $column, $post_id ) {
  21.         if ( 'featured_image' === $column ) {
  22.             $thumb = get_the_post_thumbnail(
  23.                 $post_id,
  24.                 array( 60, 60 ),
  25.                 array(
  26.                     'style' => 'display:block;width:60px;height:60px;object-fit:cover;border-radius:6px;',
  27.                     'alt'   => get_the_title( $post_id ),
  28.                 )
  29.             );
  30.             echo $thumb ?: '<span style="opacity:.6;">—</span>';
  31.         }
  32.     },
  33.     10,
  34.     2
  35. );
  36.  
  37. // Adjust column width and alignment
  38. add_action(
  39.     'admin_head-edit.php',
  40.     function () {
  41.         echo '<style>.column-featured_image{width:72px;text-align:center}</style>';
  42.     }
  43. );
  44.  
Advertisement
Add Comment
Please, Sign In to add comment