Created
September 27, 2016 09:06
-
-
Save kirandash/9a456d8792b6aef2c2ee1f848c72bfd9 to your computer and use it in GitHub Desktop.
Indexing manage_posts_custom_column. This code will add an Index value to each post which is determined by $wp_query->current_post, the number of posts per page, and the current page being viewed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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 ); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment