Add Custom Column for Posts and Custom Post Types in backend

functions.php

Default for all avalable post types.


add_image_size('featured_preview', 55, 55, true);
// GET FEATURED IMAGE
function ST4_get_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, 'featured_preview');
return $post_thumbnail_img[0];
}
}
// ADD NEW COLUMN
function ST4_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
return $defaults;
}

// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img src="' . $post_featured_image . '" />';
}
}
}
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', 'ST4_columns_content', 10, 2);

If you want Create Custom Column for your  requirement so use this code (Use for Specific Post).


add_filter('manage_edit-portfolios_columns', 'add_new_portfolios_columns');
function add_new_portfolios_columns() {
$new_columns['cb'] = '<input type="checkbox" />';
$new_columns['title'] = _x('Name', 'column name');
$new_columns['featured_image'] = __('Images');
$new_columns['taxonomy-portfolio-categories'] = __('Portfolio Categories');
$new_columns['taxonomy-portfolio-tag'] = __('Portfolio Tag');
$new_columns['date'] = _x('Date', 'column name');
return $new_columns;
}
/*Featured Images*/
// GET FEATURED IMAGE
function portfolio_get_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, 'featured_preview');
return $post_thumbnail_img[0];
}
}
// SHOW THE FEATURED IMAGE
function portfolio_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = portfolio_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img src="' . $post_featured_image . '" />';
}
}
}
// About WordPress Hooks
add_action('manage_posts_custom_column', 'portfolio_columns_content', 10, 2);

Advertisements

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: