On a project I’m doing, I created a custom post type for a catalogue and want the featured image to display on the admin table.
My custom post type is called “product” and I want to limit the feature to only the “product” custom post type. Here’s how I did it.
I added this into functions.php
// Add Post Thumbnail support add_theme_support( 'post-thumbnails' ); // Add styles to control column width add_action('admin_head', 'my_custom_table_styles'); function my_custom_table_styles() { echo '<style> .fixed #featured_thumb { width:10% } </style>'; } //// ADD FEATURED IMAGE TO PAGES AND POSTS // Add the posts and pages columns filter. They both use the same function. add_filter('manage_posts_columns', 'theme_add_post_admin_thumbnail_column', 2); add_filter('manage_pages_columns', 'theme_add_post_admin_thumbnail_column', 2); // Add the column function theme_add_post_admin_thumbnail_column($columns) { // Check if post type is 'Product' global $pagenow, $typenow; if( 'product' === $typenow && 'edit.php' === $pagenow ) { $new = array(); foreach ($columns as $key => $title) { if ($key == 'title') // Put the Thumbnail column before the Author column { $new['featured_thumb'] = __('Featured Image'); } $new[$key] = $title; } return $new; } else { return $columns; } } // Manage Post and Page Admin Panel Columns add_action('manage_posts_custom_column', 'theme_show_post_thumbnail_column', 5, 2); add_action('manage_pages_custom_column', 'theme_show_post_thumbnail_column', 5, 2); // Get featured-thumbnail size post thumbnail and display it function theme_show_post_thumbnail_column($theme_columns, $theme_id) { // Check if post type is 'Product' global $pagenow, $typenow; if( 'product' === $typenow && 'edit.php' === $pagenow ) { switch ($theme_columns) { case 'featured_thumb': if (function_exists('the_post_thumbnail')) { $permalink = get_edit_post_link(); $thumb = get_the_post_thumbnail_url(null, 'thumbnail'); echo '<a href="' . $permalink . '"><img src="' . $thumb . '" style="width:80px"></a>'; } else { echo 'Your theme doesn\'t support featured imageā¦'; } break; } } else { return $theme_columns; } } //// END ADD FEATURED IMAGE TO PAGES AND POSTS
Thanks, man. Very helpful. All I had to do was replace the CPT name.