Get the Current Page Number

In a situation where one has 20 posts per page. I would like to get the current page number in order to make some nice page links at the bottom. How do you get the current page. I tried this

<?php echo '(Page '.$page.' of '.$numpages.')'; ?>

and it just says page 1 of 1 on every page.

Any ideas,



When WordPress is using pagination like this, there’s a query variable $paged that it keys on. So page 1 is $paged=1 and page 15 is $paged=15.

You can get the value of this variable with the following code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Getting the total number of pages is a bit trickier. First you have to count all the posts in the database. Then filter by which posts are published (versus which are drafts, scheduled, trash, etc.). Then you have to divide this count by the number of posts you expect to appear on each page:

$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );

I haven’t tested this yet, but you might need to fetch $posts_per_page the same way you fetched $paged (using get_query_var()).

Leave a Comment