manage_{$post->post_type}_posts_custom_column

最后更新于:2021-11-27 06:32:08

do_action( “manage_{$post->post_type}_posts_custom_column”, string $column_name, int $post_id )

Fires for each custom column of a specific post type in the Posts list table.

参数

$column_name

(string)
The name of the column to display.

$post_id

(int)
The current post ID.

源文件

文件: gc-admin/includes/class-gc-posts-list-table.php

View on Trac


// let's say we have a CPT called 'product'
function product_custom_column_values( $column, $post_id ) {

    switch ( $column ) {

		// in this example, a Product has custom fields called 'product_number' and 'product_name'
        case 'product_number'	:
        case 'product_name' 	:
            echo get_post_meta( $post_id , $column , true );
        break;

		// in this example, $buyer_id is post ID of another CPT called "buyer"
        case 'product_buyer' 	:
            $buyer_id = get_post_meta( $post_id , $column , true );
            if( $buyer_id ){
                echo get_post_meta( $buyer_id , 'buyer_name' , true );
            } else {
                echo '<div class="dashicons dashicons-minus"></div>';
            }
        break;

    }
}
add_action( 'manage_product_posts_custom_column' , 'product_custom_column_values', 10, 2 );

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) {
    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' :
            $terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
            if ( is_string( $terms ) )
                echo $terms;
            else
                _e( 'Unable to get author(s)', 'your_text_domain' );
            break;

        case 'publisher' :
            echo get_post_meta( $post_id , 'publisher' , true ); 
            break;
    }
}