การเพิ่มคอลัมน์ที่กำหนดเองให้กับ custom post types

 

 

HOOK  เพื่อสร้างคอลัมน์ที่กำหนดเอง  และข้อมูลที่เกี่ยวข้องของพวกเขาสำหรับ custom post type  manage_{$post_type}_posts_columnsและ manage_{$post_type}_posts_custom_columnตามลำดับซึ่ง{$post_type}เป็นชื่อของ custom post type

 

ตัวอย่างนี้ จากเอกสารประกอบ จะลบคอลัมน์ผู้เขียน และเพิ่มคอลัมน์การจัดหมวดหมู่ และข้อมูล meta

// 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', 'your_text_domain' );
    $columns['publisher'] = __( 'Publisher', 'your_text_domain' );

    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)', 'your_text_domain' );
            break;

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

    }
}

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *