PHP counter: reverse numbers in the output list

I actually have a PHP loop where I’m giving to each result a number, starting from 1 and following up in ascending order. The output is as follows:

1) article C
2) article B
3) article A

Read More

…but I’d like to reverse the list number, so I get something like:

3) article C (article’s order won’t change, they are descending, by date)
2) article B
1) article A

Here’s my current loop:

<?php
if (have_posts()) :
$counter = 1;
   while (have_posts()) :
      the_post(); ?>

    <div>
        <span class="count"><?php echo $counter; ?></span>
        <?php the_title(); ?>
    </div>

<?php
$counter++;
   endwhile;
endif;
?>

Is there an easy way to do this?
Many thanks,

Related posts

Leave a Reply

3 comments

  1. If there is a function returning the posts count, e.g. count_posts() (just guessing), use it this way:

    <?php
    if (have_posts()) :
       $counter = wp_count_posts();
       while (have_posts()) :
          the_post(); ?>
    
        <div>
            <span class="count"><?php echo $counter; ?></span>
            <?php the_title(); ?>
        </div>
    
    <?php
    $counter--;
       endwhile;
    endif;
    ?>
    
  2. If this is a wordpress based site/page and the function have posts relates to the wordpress function:

    You might be better off with something like query_posts:
    http://codex.wordpress.org/Function_Reference/query_posts

    Which gives you much better control over display of the posts?

    Edit:
    Or alternatively if you use this:

    $count_posts = wp_count_posts();
    

    You can use this in tandem with the other answers by reversing your counter ($counter–;)

    That should do the trick

    http://codex.wordpress.org/Function_Reference/wp_count_posts