此代码将为每个帖子添加一个索引值,该值由$wp_query->current_post
, 每页的帖子数,以及当前正在查看的页面。
add_filter( \'manage_posts_columns\', \'wpse240648_manage_posts_columns_index\');
function wpse240648_manage_posts_columns_index($columns) {
$columns[\'index\'] = __( \'Index\', \'your-text-domain\' );
return $columns;
}
add_action( \'manage_posts_custom_column\', \'wpse240648_manage_posts_custom_column_index\', 10, 3 );
function wpse240648_manage_posts_custom_column_index( $column, $post_id ) {
if ( \'index\' == $column ) {
global $wp_query;
// This line is needed to set up the query so that $wp_query->current_post works.
$wp_query->the_post();
$page_number = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 0;
if ( $page_number > 1 ) {
$current_index = ( $page_number * $wp_query->query_vars[\'posts_per_page\'] ) - $wp_query->query_vars[\'posts_per_page\'];
} else {
$current_index = 0;
}
echo esc_html( $wp_query->current_post + 1 + $current_index );
}
}
通过适当更改挂钩名称,此代码可适用于其他帖子类型:
manage_{$post_type}_posts_columns
manage_{$post_type}_posts_custom_column