Articles
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
Trackbacks
- wp-popular.com » Blog Archive » WordPress: Quick Numbering of Paginated Posts with Results Count
- 85+ Need to Check Out Fresh Articles for Designers and Developers | Afif Fattouh - Web Specialist
- 85+ Need to Check Out Fresh Articles for Designers and Developers | Programming Blog
- Tweets that mention WordPress: Quick Numbering of Paginated Posts with Results Count | Doc4 Advertising Agency -- Topsy.com
- 85+ Need to Check Out Fresh Articles for Designers and Developers | tripwire magazine
- tripwire magazine | tripwire magazine
- WordPress: Quick Numbering of Paginated Posts with Results Count | bllogger
- WordPress: Quick Numbering of Paginated Posts with Results Count by กิ๊กก๊อก : ตามหาฝันบนโลกออนไลน์ !!!
- WordPress: Quick Numbering of Paginated Posts with Results Count | Coder Online
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
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..
Dave,
Just read your latest post. Be sure to add more is_paged() to continue counting. This is all manual.
Hopefully my math isn’t off too much.
Dave,
Try this:
All we are doing here is reading the address bar information. Let me know how that works.
Sorry for the multiple posts here.. But I’m having a problem with the “elseif” tags. They’re not working for some reason.
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?
Hey there once again…
I figured it out! Thank you so much for your help. I set it up like this…
You’re too awesome! Thank you once again!
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=2The code that I’m applying that to is
So once applied, it looks like this
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?
Dave,
Continue adding more page counts:
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.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.