Add custom column to post list table
I have a custom post type called Book
and I want to add the publisher and book author custom columns in the edit page.
I also want to fetch a specific value from each fields (I created two fields for the above columns) to display in those two additional columns. I actually can display with this code below but the point is I have to declare variable (for example: the $value
variable) for each data.
So could you please share me some ideas to display that with loop or somethings.
add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function set_custom_edit_book_columns($columns) {
$value = get_field( "contact_person" );
unset( $columns['author'] );
$columns['book_author'] = __( 'Author', 'your_text_domain' );
$columns['publisher'] = __( 'Publisher', 'your_text_domain' );
return $columns;
}
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'book_author' :
$value = get_field( "contact_person" );
$terms = get_the_term_list( $post_id , '$value' , '' , ',' , '' );
if ( is_string( $value ) )
echo $value;
else
_e( 'Unable to get author(s)', 'your_text_domain' );
break;
case 'publisher' :
echo get_post_meta( $post_id , 'publisher' , true );
break;
}
}
Topic wp-list-table custom-post-types Wordpress
Category Web