• Hello,

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • $query will be an array. If you have constructed a query for 5 posts, then the $query array will be numbered from 0 to 4. So you can either do a foreach loop and step through the array or use constructs such as $query[2]->id.

    $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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get and display post’s “position” in the query?’ is closed to new replies.