Maarten Wolfsen Maarten Wolfsen - 1 year ago 95
PHP Question

Get the current post number (not ID) in wordpress

I am using this function to get the amount of posts:

$postAmount = wp_count_posts( 'post' )->publish;

This returns 4, which is the right number.
But, is there also a function, where I can check the current post number? Not the ID, but only a number.

For example, I am at the second post, so I want the function to return '2'.

Extra information

returns 0 at every post

Answer Source

You've got the right idea, but just made a small typo. What you want is



$wp_query->current_post + 1

depending on wether or not you want to count starting at zero.

enter image description here

Search for current_post in the WP_Query Codex

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download