Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

Is there another way of counting how many itterations have been completed in the WordPress loop?

I'm curious. There are a few PHP practices (adding get variables, enqueing styles and scritps) that WordPress has it own functinos that should be used to keep the site as manageable and dynamic as possible.

So, my question is:

  • Apart from a $count variable to be incremented in the loop, does WP have its own variable to count how many itterations of a loop have been completed?

I am happy to accept 'No' as an answer :)

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Try this inside your loop:

// Echo the current index position of the loop (0,1,2,3..etc)
echo $wp_query->current_post;

You may have to change $wp_query to match whatever the correct object name is to match your code.

Liam Maclachlan
Liam Maclachlan
22,805 Points

Brilliant. Thanks man. I thought there may be something like this :)