Skip to content

Instantly share code, notes, and snippets.

@usainicola
Created December 13, 2018 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usainicola/3c16d891505641b45909f9eaa84e7a07 to your computer and use it in GitHub Desktop.
Save usainicola/3c16d891505641b45909f9eaa84e7a07 to your computer and use it in GitHub Desktop.
wp admin image column
<?php
// column post image
function add_thumb_column($cols) {
$cols['thumbnail'] = 'Image';
return $cols;
}
function add_thumb_value($column_name, $post_id) {
$width = (int) 120;
$height = (int) 120;
if ( $column_name == 'thumbnail' ) {
// thumbnail
$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 edit_post_link($thumb);
} else {
echo '-';
}
}
}
// for posts
add_filter( 'manage_posts_columns', 'add_thumb_column' );
add_action( 'manage_posts_custom_column', 'add_thumb_value', 10, 2 );
// for pages
add_filter( 'manage_pages_columns', 'add_thumb_column' );
add_action( 'manage_pages_custom_column', 'add_thumb_value', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment