wordpress 后台显示自定义文章的自定义字段的方法-文曦博客

2021-10-27 4277阅读

温馨提示:这篇文章已超过883天没有更新,请注意相关的内容是否还可用!

wordpress官网是这样描述的:

wordpress 后台显示自定义文章的自定义字段的方法-文曦博客  第1张

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

   wordpress官网例子:

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;
    }}

我写的,列出了文章ID,也可以显示自定义字段:

//添加面板manage_chengyuan_posts_custom_columnadd_filter( 'manage_chengyuan_posts_columns', 'set_custom_edit_book_columns' );//添加表头function set_custom_edit_book_columns($columns) {
    unset( $columns['author'] );
    $columns['post_id'] = __( 'ID', '用户ID' );
    return $columns;}add_action( 'manage_chengyuan_posts_custom_column' , 'custom_book_column', 10, 2 );//显示每行内容function custom_book_column( $column, $post_id ) {
    switch ( $column ) {
 
        case 'post_id' :
                echo $post_id;
            break;
    }}//添加面板

        我的截图:

wordpress 后台显示自定义文章的自定义字段的方法-文曦博客  第2张