WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Front page post count (8 posts)

  1. Jaxia
    Member
    Posted 9 years ago #

    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?

  2. skippy
    Member
    Posted 9 years ago #

    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;
    ?>

  3. Jaxia
    Member
    Posted 9 years ago #

    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

  4. Kafkaesqui

    Posted 9 years ago #

    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.

  5. Jaxia
    Member
    Posted 9 years ago #

    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?

  6. skippy
    Member
    Posted 9 years ago #

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

  7. Jaxia
    Member
    Posted 9 years ago #

    Awesome! Wish me luck ;)
    I will let you know how it goes...

  8. Jaxia
    Member
    Posted 9 years ago #

    That worked perfectly! Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.