0

I am trying to customize the category template whereby I wish to show the ordered list of 10 posts on each page. I am using the following code in the loop:

<?php $counter = 1; 
while(have_posts()) : the_post(); ?> 
    <div class="entry">
        <?php echo $counter++; the_excerpt(''); ?>
    </div> 
<?php endwhile; ?>

On the first page: it shows the excerpts arranged in a 1 to 10 ordered list, but on the second page (using wp-pagenavi), it again starts from 1. Is there any way that all the posts of a category can be shown in a consistent ordered list from post 1 to last post.

1
  • Don't trust this 100% percent, but I'm pretty sure you can get_query_var('posts_per_page') * (get_query_var('paged')) ? get_query_var('paged') : 1 + your counter. In essence, which page am I at? times how many posts do I display on each page? plus this is the how-many'eth post on this page?
    – isotrope
    Jan 10, 2013 at 14:40

1 Answer 1

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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