Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '13 at 14:40
add comment

1 Answer

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.

share|improve this answer
    
This worked perfectly :) Thanks a lot. –  Suresh Soni Jan 11 '13 at 6:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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