52 | Numbering Posts


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

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.

This entry was posted in Easy Coding for Wordpress and tagged , . Bookmark the permalink.

3 Responses to Numbering Posts

  1. cogmios comments:

    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. pieter comments:

    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

    • alchymyth replies:

      ‘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 to cogmios Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.