Add Custom Post Type Columns for wordpress

这儿以 costom post portfolio 为例, 为 portfolio 列表页加上 两个分类列。删除时间列。

add_filter( 'manage_edit-portfolio_columns', 'set_custom_edit_portfolio_columns' );
add_action( 'manage_portfolio_posts_custom_column' , 'custom_portfolio_column', 10, 2 );

function set_custom_edit_portfolio_columns($columns) { 
    unset($columns['date']); //注销不需要的列
    return $columns + array('portfolio_category' => __('Categories'),'client_category'=>'Client Terms');
}

function custom_portfolio_column( $column, $post_id ) {
    switch ( $column ) {
      case 'portfolio_category':
        $terms = get_the_term_list( $post_id , 'portfolio_category' , '' , ',' , '' );
        if ( is_string( $terms ) ) {
            echo $terms;
        } else {
            echo 'Unable to get Category(s)';
        }
        break;
        
      case 'client_category':
        $cterms = get_the_term_list( $post_id , 'client_category' , '' , ',' , '' );
        if ( is_string( $cterms ) ) {
            echo $cterms;
        } else {
            echo 'Unable to get Client Terms';
        }
        break;        
    }
}