WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] counter to count backwards for php (10 posts)

  1. gbaka
    Member
    Posted 4 years ago #

    I'm trying to figure out how to make my counter to count backwards in the while loop for my post so when i echo it get 3 2 1 order, in order of post creation instead of 1 2 3 which isn't in post order.

  2. gbaka
    Member
    Posted 4 years ago #

    so anyone?

  3. gbaka
    Member
    Posted 4 years ago #

    anyone?

  4. Can you share a link to your blog? What theme are you using? Is the counter a plugin or hard-coded?

  5. gbaka
    Member
    Posted 4 years ago #

    its php code as in $counter++ and I'm echoing it in the while post loop to get numbers but its going 1 2 3 4 order and i want it in the reverse order so the first post will remain as 1 instead of changing to a 2 so in short i want it to go 4 3 2 1...

  6. Thanks, we also need to know what theme you're using. And, if you share a link to your blog, it would help quite a bit.

  7. gbaka
    Member
    Posted 4 years ago #

    I'm using a custom theme and this is basically the code

    <?php $titlecount = 0; ?>
    
    <?php while (have_posts()) : the_post(); ?>
    <?php $titlecount++; ?>
    
    <?php echo $titlecount; ?>
    
    <?php endwhile; ?>

    this is in the archive.php

  8. gbaka
    Member
    Posted 4 years ago #

    anyone?

  9. jrav001
    Member
    Posted 4 years ago #

    Loop through all the posts (without displaying them) to count the total number to display.

    Now that you have a total for $titlecount, run the loop again, but this time, decrease the count using $titlecount--

  10. gbaka
    Member
    Posted 4 years ago #

    thank you that worked like a charm!

Topic Closed

This topic has been closed to new replies.

About this Topic