WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Front page post count

[Resolved] Front page post count

  • Hello All –

    When I set my blog up to post 10 entries on the first page, how does it count those entries? Does it track the number somewhere?

    I’m looking for something that will look at a post and go “This is the first post on the page, assign ID of 1, this is the second post, assign ID of 2” and so on.

    Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The collection of posts is assembled just before The Loop begins, based on the criteria passed on the URL.

    The easiest way to count which relative post you’re displaying through The Loop is to use a temporary counter variable, like this:

    <?php
    $counter = 1;
    // Begin the Loop
    if (have_posts()) :
    while (have_posts()) : the_post();
    ...
    // just before the end of the loop, increase our counter variable
    $counter++;
    //end the Loop
    endwhile;
    ?>

    Skippy,

    I was hoping there was an easy answer! Thank you so much! You made my day! Can I use that exactly, or do I need to do other stuff to it?

    Thanks again,
    ~Jax

    Skippy’s code will work fine as is.

    The main elements to pay attention to are 1. setting $counter before The Loop is initialized, and 2. incrementing ($counter++) in The Loop but at the end.

    Very cool!
    But how can I call the number? I need to output the number that it assigns to each post with the post.

    <? php counter ?>
    or something like that?

    This:
    <?php echo $counter; ?>
    will output the current value of the counter.

    Awesome! Wish me luck 😉
    I will let you know how it goes…

    That worked perfectly! Thank you so much!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Front page post count’ is closed to new replies.
Skip to toolbar