Reputation: 9
I have added code to my function.php
file to add the Featured Image of a post to my admin column. It works great for posts and pages, but for my two custom post types (cars, wheels) it doesn't do anything to the admin layout.
Can someone help me with this? Do I need to add a filter for each custom?
I got this code from here: Add featured image thumbnail to WordPress admin columns
The following code in my function.php
file:
// Add the posts and pages columns filter. They can both use the same function.
add_filter('manage_posts_columns', 'tcb_add_post_thumbnail_column', 5);
add_filter('manage_pages_columns', 'tcb_add_post_thumbnail_column', 5);
add_filter('manage_custom_post_columns', 'tcb_add_post_thumbnail_column', 5);
// Add the column
function tcb_add_post_thumbnail_column($cols){
$cols['tcb_post_thumb'] = __('FeaTured');
return $cols;
}
// Hook into the posts an pages column managing. Sharing function callback again.
add_action('manage_posts_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);
add_action('manage_custom_post_column', 'tcb_display_post_thumbnail_column', 5, 2);
// Grab featured-thumbnail size post thumbnail and display it.
function tcb_display_post_thumbnail_column($col, $id){
switch($col){
case 'tcb_post_thumb':
if( function_exists('the_post_thumbnail') )
echo the_post_thumbnail( 'admin-list-thumb' );
else
echo 'Not supported in theme';
break;
}
}
Upvotes: 0
Views: 5377
Reputation: 2947
The below code I created for my gallery post type and it is working 100% you can change gallery to you post type name.
first of all add thumbnails support. and image size for the preview
add_theme_support( 'post-thumbnails' );
add_image_size( 'gallery-post-prev', 50, 50, true );
then set the thumbnail.
now create a function in your functions.php to get the featured imgae
/**
* get featured image function
*/
function gallery_featured_image($post_ID) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id) {
$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'gallery-post-prev');
return $post_thumbnail_img[0];
}
}
Now create a column head, this is the heading for the column, in our case it is "featured image"
/**
* add column heading
*/
function gallery_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
return $defaults;
}
now create column content, in our case we will display the featured image in the column.
/**
* show featured image in column
*/
function gallery_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = gallery_featured_image($post_ID);
if ($post_featured_image) {
echo '<img src="' . $post_featured_image . '" />';
}
}
}
now add the filter for showing column heading we created
add_filter('manage_gallery_posts_columns', 'gallery_columns_head', 10);
and add action hook for showing the featured image in the column content.
`add_action('manage_gallery_posts_custom_column', 'gallery_columns_content', 10, 2);`
Upvotes: 1
Reputation: 12111
I was implementing a custom theme with custom post types and found that I needed to add the support to the post type as well as declare it in the theme. As below:
register_post_type( 'team',
array(
'labels' => array(
'name' => __( 'Team Members' ),
'singular_name' => __( 'Team Member' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
)
);
add_theme_support( 'post-thumbnails', array( 'team' ) );
Note that it's in the register_post_type() arguments as 'supports' and explicitly declared in the add_theme_support() call.
Enjoy your featured images!
Upvotes: 0
Reputation: 36
Have you enabled thumbnail support in the post type?
For example, my wp-glossary plugin registers a glossary post type that has posts capabilities (default) and thumbnails enabled, and it works out of the box:
add_action('init', 'tcb_glossary_register_posttype_glossary');
function tcb_glossary_register_posttype_glossary() {
register_post_type( 'glossary',
array(
'labels' => array(
'name' => __( 'Glossary Terms' ),
'singular_name' => __( 'Glossary Term' ),
'add_new' => __( 'Add New Term' ),
'add_new_item' => __( 'Add New Glossary Term' ),
'edit_item' => __( 'Edit Glossary Term' ),
'new_item' => __( 'Add New Glossary Term' ),
'view_item' => __( 'View Glossary Term' ),
'search_items' => __( 'Search Glossary Terms' ),
'not_found' => __( 'No Glossary Terms found' ),
'not_found_in_trash' => __( 'No Glossary Terms found in trash' )
),
'public' => true,
'menu_position' => 105,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'has_archive' => true,
)
);
flush_rewrite_rules( false );
}
Thanks for visiting my site and trying my snippet :)
Upvotes: 1