So this got me for a minute. You manage your custom columns in WordPress using add_filter.
First you have to add them:
add_filter('manage_posts_columns', 'add_new_columns');
function add_new_columns($columns) {
$columns['thumb'] = __('Thumb');
return $columns;
}
…etc.
Then you populate them (in my case, add post type to any column named ‘thumb’):
add_action( 'manage_posts_custom_column', 'manage_columns' );
function manage_columns($column_name, $post_id) {
switch ($column_name) {
case 'thumb':
if(has_post_thumbnail($post_id)){
echo get_the_post_thumbnail($post_id,array(50,50));
}else{
echo 'No Featured Image';
}
break;
default:
break;
}
}
This is what got me
If your post types are hierarchical, then WordPress considers them ‘pages’. If that is the case you have to use:
add_action( 'manage_pages_custom_column', 'manage_columns' );
This will add the action to all hierarchical post types (pages) instead of all non-hierarchical post types (posts). If you want to target your custom post types only, hierarchical or not, you can do so using:
add_action( 'manage_{custom-post-type}_posts_custom_column', 'manage_columns' );
I know we are moving fast, so… a quick rundown:
Global non-hierarchical post types (posts) use:
add_action( 'manage_posts_custom_column', 'manage_columns' );
Global hierarchical post types (pages) use:
add_action( 'manage_pages_custom_column', 'manage_columns' );
Specific post types hierarchical or non-hierarchical (custom post types) use:
add_action( 'manage_{custom-post-type}_posts_custom_column', 'manage_columns' );
Hope that saves someone a few minutes.
3 Comments:
Comments have been closed for this post. If you would like to ask a question or need help, please post in the forums.