Advertisement
Guest User

WordPress custom post columns

a guest
Sep 15th, 2011
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. // reset the column fields and order of the post list table
  2. add_filter('manage_edit-post_columns', 'manage_edit_post_columns');
  3. function manage_edit_post_columns($columns) {
  4.     unset($columns['tags']); // optional, removes the "Tags" column
  5.  
  6.     // add a custom column with a label of "My Custom Taxonomy Column" and slug of my_custom_taxonomy
  7.     $columns['my_custom_taxonomy'] = __('My Custom Taxonomy Column');
  8.  
  9.     return $columns;
  10. }
  11.  
  12. // tell WordPress what to display in the post list table column we created using the 'manage_edit-post_columns' hook
  13. add_action( 'manage_posts_custom_column', 'manage_edit_post_column', 10, 2);
  14. function manage_edit_post_column($column_name, $post_id ) {
  15.     $post = get_post($post_id);
  16.  
  17.     switch ($column_name) {
  18.         case 'my_custom_taxonomy':
  19.             unset($medgadget_display_term_links);
  20.             $my_custom_taxonomy_terms = get_the_terms( $post_id, 'my_custom_taxonomy_slug' );
  21.  
  22.             if ( !empty($my_custom_taxonomy_terms) ) {
  23.                 $out = array();
  24.                 foreach ($my_custom_taxonomy_terms as $term) {
  25.                     $out[] = sprintf( '<a href="%s">%s</a>',
  26.                         esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'my_custom_taxonomy_slug' => $term->slug ), 'edit.php' ) ),
  27.                         esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'my_custom_taxonomy_slug', 'display' ) )
  28.                     );
  29.                 }
  30.                 echo join( '<br />', $out );
  31.             }
  32.             else {
  33.                 echo 'No terms found';
  34.             }
  35.             break;
  36.         case 'another_custom_column' :
  37.             // example of how to add multiple custom columns, so let's not display anything right now
  38.             break;
  39.         default:
  40.             break;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement