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 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 similar 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.

Dale Crum

Dale Crum

Owner / Creative Director at Doc4 Design