NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 11 2022
Estimated reading time : 1 minute, 25 seconds - 147 words
Share the post "Add an Image Post Type Columns in the Admin"
With this tutorial, we will show you how to add a column for your post’s thumbnail and display it in your Admin post type.
Let’s say our post type is called viavideos. We will add a new filter to create the column for the image.
In our filter manage_viavideos_posts_columns, you’ll notice that you need your post type. It is vital that your post type is specified so it can be executed properly.
Then we build our function:
add_filter('manage_viavideos_posts_columns', 'via_videos_columns', 5); function via_videos_columns($defaults){ $defaults['via_videos_post_thumbs'] = __('Image'); return $defaults; }
Then we build the action. We need to fetch the via_videos_post_thumbs data where our the_post_thumbnail will be displayed.
Add an Image Post Type Columns in the Admin
add_action('manage_viavideos_posts_custom_column', 'posts_via_videos_columns', 5, 2); function posts_via_videos_columns($column_name, $id){ if($column_name === 'via_videos_post_thumbs'){ echo the_post_thumbnail( array(50,50), true); } }
The condition to display is if the name of the column === via_videos_post_thumbs, so it displays post_thumbnail.
Note that the function includes a width = 50 and height = 50. You can also call the post_thumbnail this way: echo the_post_thumbnail(‘thumbnail’);