Numbering Posts

To give posts a continuous number in index pages or archives, try this little piece of code:

<?php
global $query_string;
  $all_post_query = new WP_Query($query_string.'&posts_per_page=-1');
    $count_posts = $all_post_query->post_count;
  $this_post_nr = 1 + $wp_query->current_post + (get_query_var('paged')?(get_query_var('paged')-1) : 0) * get_option('posts_per_page');
echo $this_post_nr.' of '.$count_posts.' in this selection of posts.';
?>

Place it within the loop;
condition: needs to be adjusted for any custom query.

3 Replies to “Numbering Posts”

  1. grin, maybe something less memory intensive might be better…
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /xx/wp-includes/wp-db.php on line 1539 ….

    Maybe better to cache this count offline.

  2. Hi, this is exactly what I’m looking for, the problem is, in which file do I need to paste this code. I’m still learning wp, so it will be appreciated if you can help. I am working on a child theme as well for twenty eleven. Many thanks

    1. ‘Place it within the loop;’ – in a child theme of Twenty Eleven, this could be in content.php. where exactly depends on where you want to see the number. possibly directly after: <h1 class="entry-title">
      consider joining the WordPress.org support forum to ask any WordPress related questions.

Leave a Reply

Your email address will not be published. Required fields are marked *