Ever wanted to display a post attachment count in an admin column? This snippet will display the number of post attachments for each post in a custom admin column.
Instructions
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5); add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2); function posts_columns_attachment_count($defaults){ $defaults['wps_post_attachments'] = __('Attached'); return $defaults; } function posts_custom_columns_attachment_count($column_name, $id){ if($column_name === 'wps_post_attachments'){ $attachments = get_children(array('post_parent'=>$id)); $count = count($attachments); if($count !=0){echo $count;} } }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out How to Set Up Download Tracking in WordPress With Google Analytics.
how can i implement it for custom post type?
[…] Source: WPSNIPP […]
Very cool!
glad to see you like the snippet!
[…] to Kevin Chard for the […]