wordpress 后台显示自定义文章的自定义字段

        wordpress官网是这样描述的:

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_column
add_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 后台显示自定义文章的自定义字段  第1张

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容