I have a simple query: $query = new WP_Query('showposts=5');
that will obviously display 5 latest posts. Is there any way to be able to get post’s position in the query? By position I mean… $query has 5 posts, and I need to be able to display the number of a post in the loop. I don’t really know PHP but I’m assuming the $query is an array variable (?) with these 5 posts.
It’s gonna be used in the JavaScript slider thing, where for every post I display a link like <a href="#1"></a>
and I need that number to be 2 for the second post, 3 for the third etc.
Hope that makes any sense and someone will be able to help me.
Thanks in advance,
Justine
For more bulletproof behavior, I would create the anchor links using each post’s UID (using
the_ID()
), rather than by their position on the page.Additionally, you should be iterating through
$query
be using the loop, which you can do multiple times in a page. (Haven’t used WordPress in a while, this code may be a bit off but the concept is sound)$query->current_post will give you the index of the current item in the loop. Also, $query->post_count gives you the total count of the items in the loop. That might be helpful as well.
It’s not that hard (I copied the PHP-code of cpharmston):
You can use $i for #1, #2 etc. Everytime the while-loop has come to the end, $i++ makes sure it increments with one (so after the first time $i = 2 etc.).
Hope this helps 🙂 (I do think that cpharmston’s solution would be easier though).