Ben Moseley
Ben Moseley

Reputation: 136

How to number posts in order displayed in loop

all I'm trying to do is get the number of the order in which the post is being displayed in the wordpress loop. I'm not looking for post-ID. To give you a better idea of what I'm trying to do, I'll show you an example:

<div class="post" id="post-1>
    ...
</div>
<div class="post" id="post-2>
    ...
</div>

The reason being is I want to style posts by their given order in the loop.

Upvotes: 0

Views: 832

Answers (1)

Dan Grossman
Dan Grossman

Reputation: 52372

Before "The Loop" begins:

<?php $number = 1; ?>

Inside "The Loop" but after your HTML:

<?php $number++; ?>

Then anywhere in your HTML, you can output the post number by printing the value of $number.

<?php echo $number; ?>

Example in a WordPress theme file:

<?php $number = 1; ?>

<?php while ( have_posts() ) : the_post(); ?>

    <div class="post" id="post-<?php echo $number; ?>">
        ...
    </div>

    <?php $number++; ?>

<?php endwhile; ?>

Upvotes: 3

Related Questions