0

In the Loop use this code

<?php $count = $wp_query->current_post;  echo $count; ?>

On first page post counts from 0 to 14, but from second page post counts from 0 to 14 to. How use global counter for count post on second from 15 to 29, third from 30 to 44 ...

  • 2
    You need to use get_option( 'posts_per_page' ) and get_query_var( 'paged' ) to calculate your post numbers – Pieter Goosen Apr 27 '15 at 10:31
  • @PieterGoosen Thnx for answer, maybe you have little example? – KingStakh Apr 27 '15 at 11:24
1

As I already stated in a comment,

You need to use get_option( 'posts_per_page' ) and get_query_var( 'paged' ) to calculate your post numbers

You can do something like the following: (I have commented the code to make it easy to follow. This goes into functions.php)

function get_post_number()
{
    global $wp_query;

    /*
     * Get current page number. Set page 1 to one as get_query_var( 'paged' ) will be 0
     */
    $current_page_number   = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    /* 
     * Get the posts_per_page option that is set under "Reading"
     */
    $posts_per_page        = get_option( 'posts_per_page' );
    /*
     * Get the current post position in the loop, add 1 because the counter starts at 0
     */
    $current_post_position = $wp_query->current_post + 1;

    /*
     * If this is page one, return the post position as is
     */
    if ( $current_page_number == 1 )
        return $current_post_position;

    /*
     * Calculate the post number on paged pages
     */
    return ( $posts_per_page * ( $current_page_number - 1 ) ) + $current_post_position;
}

You can then use it as follow in your templates to display the correct post numbers (You need to use this inside the loop)

echo get_post_number();
  • What would you use this for? – Brad Dalton Apr 27 '15 at 12:18
  • 1
    I think from the OP it is quite clear that this will display the current post post number :-) – Pieter Goosen Apr 27 '15 at 12:19
  • @BradDalton I use this for Leaflet Map, to show post on map whit popup open. Can see here 1871.by (but in russian) – KingStakh Apr 27 '15 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.