admin管理员组

文章数量:1291030

Based on this post, I added the code snippet below to my Wordpress site, as to display the ID in the Posts section of each post. However, it returns "critical error". Any idea what is wrong with this code?

add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_filter( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 3 );

function column_register_wpse_101322( $columns ) {
    $columns['uid'] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $empty, $column_name, $post_id ) {
    if ('uid' != $column_name) {return $empty;}
    return "$post_id";
}

Based on this post, I added the code snippet below to my Wordpress site, as to display the ID in the Posts section of each post. However, it returns "critical error". Any idea what is wrong with this code?

add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_filter( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 3 );

function column_register_wpse_101322( $columns ) {
    $columns['uid'] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $empty, $column_name, $post_id ) {
    if ('uid' != $column_name) {return $empty;}
    return "$post_id";
}
Share Improve this question edited Jun 5, 2021 at 16:42 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jun 5, 2021 at 16:04 NickNick 1272 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Unfortunately you can't just copy code that plays a completely different role and replace users with posts and somehow expect this to do what you want it. The reason you get a fatal error is because you're passing in too many arguments (3) to the filter, when there are only 2 supplied.

Here's the code to achieve what you need, if I understand you correctly:


add_filter( 'manage_posts_columns', 'column_register_wpse_101322' );
add_action( 'manage_posts_custom_column', 'column_display_wpse_101322', 10, 2 );

function column_register_wpse_101322( $columns ) {
    $columns[ 'uid' ] = 'ID';
    return $columns;
}

function column_display_wpse_101322( $column_name, $post_id ) {
    if ( 'uid' === $column_name ) {
        echo $post_id;
    }
}

本文标签: Code snippet to display ID gives critical error