2

I am trying to add a column in the edit post screen that shows any attached media. I found this code to do it with tags, but don't know how to change it to do Media attachments instead.

//add media column to edit post screen add_action('media_buttons_context','zg_post_buttons'); function add_tag_column($posts_columns) { // Add a new column $posts_columns['att_tag'] = _x('Tags', 'column name'); return $posts_columns; } function manage_attachment_tag_column($column_name, $id) { switch($column_name) { case 'att_tag': $tagparent = "upload.php?"; $tags = get_the_tags(); if ( !empty( $tags ) ) { $out = array(); foreach ( $tags as $c ) $out[] = "<a href='".$tagparent."tag=$c->slug'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_tag', 'display')) . "</a>"; echo join( ', ', $out ); } else { _e('No Tags'); } break; default: break; } 

1 Answer 1

2

you can create a column of post_thumbnail like so:

// ADDING THUMBNAIL TO EDIT SCREEN if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) { // for post and page add_theme_support('post-thumbnails', array( 'post', 'page' ) ); function fb_AddThumbColumn($cols) { $cols['thumbnail'] = __('Thumbnail'); return $cols; } function fb_AddThumbValue($column_name, $post_id) { $width = (int) 100; $height = (int) 100; if ( 'thumbnail' == $column_name ) { // thumbnail of WP 2.9 $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true ); // image from gallery $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') ); if ($thumbnail_id) $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true ); elseif ($attachments) { foreach ( $attachments as $attachment_id => $attachment ) { $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true ); } } if ( isset($thumb) && $thumb ) { echo $thumb; } else { echo __('None'); } } } // for posts add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' ); add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 ); // for pages add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' ); add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 ); } 

and to add this column to custom post type just add your post type to

add_theme_support('post-thumbnails', array( 'post', 'page','mycustom' ) ); 

and and action and filter hooks

add_filter( 'manage_mycustom_posts_columns', 'fb_AddThumbColumn' ); add_action( 'manage_mycustom_posts_custom_column', 'fb_AddThumbValue', 10, 2 ); 

Hope this helps

3
  • are you kidding? That is awesome. But what if it's a video or audio media attachment? Commented Feb 16, 2011 at 17:25
  • Thank you for this piece of code. Works perfectly by copy-pasting into the themes functions.php :-) Commented Aug 16, 2012 at 13:18
  • What happened to the markup there? :) Commented Jul 11, 2014 at 20:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.