本文作者:温文曦

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

温文曦 2021-10-21 957 抢沙发
wordpress 后台显示自定义文章的自定义字段摘要:         wordpress官网是这样描述的:do_action( "ma...

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

文章版权及转载声明

作者:温文曦本文地址:https://www.wxnotes.com/blog/648.html发布于 2021-10-21
文章转载或复制请以超链接形式并注明出处文曦博客

赞(24)
阅读
分享

发表评论

快捷回复:

评论列表 (暂无评论,957人围观)参与讨论

还没有评论,来说两句吧...