Counter code for paginated category pages in wordpress

Yes, of course it starts again at 1. You’ve initialized the counter to 1 on each page load.

If you were writing a custom loop you’d do this:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

If normal WordPress pagination mechanisms are working on your page that should work for you too. That will number your pages. Your counter should start at $paged times your posts per page, minus your posts per page. So with ten posts per page…

  1. Page 1 -> $paged = 1 -> 1 * 10 = 10 -> 10 minus 10 = 0 -> Start at zero
  2. Page 2 -> $paged = 2 -> 2 * 10 = 20 -> 20 minus 10 = 10 -> Start at 10

Of course humans start counting at 1 but if you increment the counter before you print it, problem solved.

<?php 
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$counter = ($mypage * $ppp) - $ppp; 
while(have_posts()) : the_post(); 
    $counter++; // that pushes the zero based counter up one ?> 
    <div class="entry">
        <?php echo $counter; the_excerpt(''); ?>
    </div> 
<?php endwhile; ?>

Untested. Maybe buggy. But I am pretty sure it is on the right track.

Leave a Comment