Tutorial: WordPress
05. January 2010 · Author: Dale Crum
9 Comments

WordPress: Quick Numbering of Paginated Posts with Results Count

In this tutorial we will be covering a simple method of adding numbers to looped posts. An example usage of this technique is a search results page where the output may span multiple pages.

Why not use an Ordered List on the Loop and let it run its course? This is where the problem actually began. By using an Ordered List on the Loop we are able to number the posts with ease, however when proceeding to the second or third page we noticed that the Ordered List actually begins again each time. This is not very effective when using a Google style counter such as Matthew Taylor’s Results Count plugin where a set of results may display something to the effect of “1-25 of 100 results”.

One very simple method to achieve our goal is to use php in order to determine the current page and then begin counting at a pre-determined number. For example, if each page of results will display 50 posts then the second page should begin at number 51 with each additional page counting upward instead of reverting back to one.

The code:

<?php echo $count; ?> <?php $count = $count+1; ?>

This is the initial php instruction informing the Loop to continue counting each post, very similar to an Ordered List. The actual number of posts, or results as the case may be, per page are still commanded from the WordPress Settings > Reading > “Blog pages show at most”. Keep this in mind while working through the tutorial. Begin by placing the code in the Loop. This tutorial goes under the assumption you have a basic understanding of the WordPress Loop, if not see the WordPress codex page: read more. Below we have provided a simplified Loop for our needs.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <?php echo $count; ?>
  <!-- do stuff ... -->
 <?php $count = $count+1; ?>
<?php endwhile; endif; ?>

Begin by initializing the count before the Loop by using $count and towards the end of the Loop we force the count each time it is run using $count+1. The problem with this is that the counting begins and ends with each page again the same issue as using an Ordered List. The solution is to inform the code to check the address bar to determine the current page, then based on this information, begin counting at a at a new number. To achieve this, we will write additional code prior to the Loop. The result will look similar to the following:

<?php if((preg_match('/\/2\//',$_SERVER['REQUEST_URI']))) {
 $count = 51;
} elseif((preg_match('/\/3\//',$_SERVER['REQUEST_URI']))) {
 $count = 101;
} else {
 $count = 1;
} ?>

Once in place the final code will look similiar to this:

<?php if((preg_match('/\/2\//',$_SERVER['REQUEST_URI']))) {
 $count = 51;
} elseif((preg_match('/\/3\//',$_SERVER['REQUEST_URI']))) {
 $count = 101;
} else {
 $count = 1;
} ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <?php echo $count; ?>
  <!-- do stuff ... -->
 <?php $count = $count+1; ?>
<?php endwhile; endif; ?>

Don’t be afraid to add additional code to keep the count moving. It is simple to check the total number of posts by visiting the Dashboard “Right Now” feature to get a general ending point. Hopefully this tutorial was helpful and if you have ideas on how to improve this code we will be happy to include it.

9 Comments

  1. Bac says:

    I’m using infinite scroll which loads the next page via ajax so the page number does not change… It does wrap the page in a div called page 2 tho

    wondering if you could think of a way of getting round this for both JS on and off? I’ve tried and failed!

    Cheers,
    bac

  2. Dave says:

    Hey Guys,

    I tried both of those solutions and for some reason which ever is declared first, it stays like that after that condition is met. So after paged 2… the count stops and it doesn’t acknowledge paged 3 :(

    This is driving me nutssssss. I’m still trying to figure it out. I tried using conditional statements as well with using that !not but I still get the same effect where it won’t go past paged 2..

  3. Dave,

    Just read your latest post. Be sure to add more is_paged() to continue counting. This is all manual.

    <?php if((is_paged(2))) {
     $count = 51;
    } elseif((is_paged(3))) {
     $count = 102;
    } elseif((is_paged(4))) {
     $count = 153;
    } elseif((is_paged(5))) {
     $count = 204;
    } else {
     $count = 1;
    } ?>
    

    Hopefully my math isn’t off too much.

  4. Dave,

    Try this:

    <?php if((preg_match('/\/?paged=2\//',$_SERVER['REQUEST_URI']))) {
     $count = 51;
    } elseif((preg_match('/\/?paged=3\//',$_SERVER['REQUEST_URI']))) {
     $count = 101;
    } elseif((preg_match('/\/?paged=4\//',$_SERVER['REQUEST_URI']))) {
     $count = 152;
    } else {
     $count = 1;
    } ?>

    All we are doing here is reading the address bar information. Let me know how that works.

  5. Dave says:

    Sorry for the multiple posts here.. But I’m having a problem with the “elseif” tags. They’re not working for some reason.

    
    <?php if((is_paged(2))) {
     $count = 51;
    } elseif((is_paged(3))) {
     $count = 101;
    } else {
     $count = 1;
    } ?>
    

    Once it gets to page 3, it stays on 51 and doesn’t go any higher than that on any page after that. Any idea on how to find a solution for this?

  6. Dave says:

    Hey there once again…

    I figured it out! Thank you so much for your help. I set it up like this…

     
    <?php if((is_paged(2))) {
     $count = 51;
    } elseif((is_paged(3))) {
     $count = 101;
    } else {
     $count = 1;
    } ?>
    <?
    if (have_posts()) : while(have_posts()): the_post(); ?>
    <a href="#" onclick="scrollToEntry(<?the_ID(); ?>)"><?php echo $count; $count++;?></a>
    
    <?php endwhile; endif; ?>
    

    You’re too awesome! Thank you once again!

  7. Dave says:

    Hey Doc,

    Thanks for getting back to me so soon. No one ever gets back to me on any blog! Haha.

    Um, I tried to add that to my code but I was unsuccessful. My links look like this so maybe that’s why it won’t work?

    http://website.com/wordpress/?paged=2

    The code that I’m applying that to is

    
    <?php 
    $count = 1; 
    if (have_posts()) : while(have_posts()): the_post(); ?>
    post numbers get generated here
    <a href="#" onclick="scrollToEntry(<?the_ID(); ?>)"><?php echo $count; $count++; ?></a>
    
    <?php endwhile; endif; ?>
    

    So once applied, it looks like this

    
    <?php if((preg_match('/\/2\//',$_SERVER['REQUEST_URI']))) {
     $count = 51;
    } elseif((preg_match('/\/3\//',$_SERVER['REQUEST_URI']))) {
     $count = 101;
    } else {
     $count = 1;
    } ?>
    
    <?php 
    $count = 1; 
    if (have_posts()) : while(have_posts()): the_post(); ?>
    post numbers get generated here
    <a href="#" onclick="scrollToEntry(<?the_ID(); ?>)"><?php echo $count; $count++; ?></a>
    
    <?php endwhile; endif; ?>
    

    It’s more than likely that I’m doing something wrong here. Do you think you could help me out incorporating the additional stuff needed to make this complete?

  8. Dave,

    Continue adding more page counts:

    
    <?php if((preg_match('/\/2\//',$_SERVER['REQUEST_URI']))) {
     $count = 51;
    } elseif((preg_match('/\/3\//',$_SERVER['REQUEST_URI']))) {
     $count = 101;
    } elseif((preg_match('/\/4\//',$_SERVER['REQUEST_URI']))) {
     $count = 152;
    } else {
     $count = 1;
    } ?>

    Note that the number in this line (preg_match('/\/2\//',$_SERVER['REQUEST_URI') represents something that will be shown in the address bar. For example: http://www.doc4design.com/articles/page/2/ This cold be anything from a word such as “articles” to a number such as “3″. The trick here is to find something that is unique to the page which you are on. This is why we have chosen the page number itself.

  9. Dave says:

    Hey there, I wanted to know how I could get this to keep the count going once I hit page 2,3, etc… Any help would be great.

Leave A Comment

Please wrap any code within the <code> tag to display properly

Multicons is a multi-favicon code generator which automatically inserts the necessary meta tags for both favicons (site-wide and/or admin) and Apple Touch icons. Not sure what a...

IE6 Upgrade Option utilizes the 7.9kb script created by mihai.ile: http://code.google.com/p/ie6-upgrade-warning/ as a WordPress plugin. I preferred this script over the many alternatives...

The WordPress Plugin: WordPress Database Backup by Austin Matzko is one of the more intuitive backup plugins currently available and with no stern warnings to scare off the faint of...

While doing our usual run of site updates and code adjustments, we ran into a small issue: how to display WordPress bookmarks with both text and images. Utilizing the 'Links' tab (...

BookMaster is our answer to what we feel is an odd issue with the WordPress wp_list_bookmarks tag. For those that have exercised the use of wp_list_bookmarks, you are well aware that...