Created
July 28, 2022 08:26
-
-
Save ragoand/5cec23a3c12e7baba2b77b96ecea9546 to your computer and use it in GitHub Desktop.
Add cutom columns to a WP List Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the custom columns to the book post type: | |
add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' ); | |
function set_custom_edit_book_columns($columns) { | |
unset( $columns['author'] ); | |
$columns['book_author'] = __( 'Author', 'textdomain' ); | |
$columns['publisher'] = __( 'Publisher', 'textdomain' ); | |
return $columns; | |
} | |
// Add the data to the custom columns for the book post type: | |
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 ); | |
function custom_book_column( $column, $post_id ) { | |
switch ( $column ) { | |
case 'book_author' : | |
$terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' ); | |
if ( is_string( $terms ) ) | |
echo $terms; | |
else | |
_e( 'Unable to get author(s)', 'textdomain' ); | |
break; | |
case 'publisher' : | |
echo get_post_meta( $post_id , 'publisher' , true ); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment