Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

Marvellous

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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()).

share|improve this answer
3  
Why not use $wp_query->max_num_pages? –  t31os Mar 18 '11 at 15:47
    
That's a sound alternative. Part of $wp_query I completely forgot was there :-/ –  EAMann Mar 18 '11 at 17:49

You could do it with a single line of code, but then again, you might want to add the code in other places, so a function is usually more useful.

function current_paged( $var = '' ) {
    if( empty( $var ) ) {
        global $wp_query;
        if( !isset( $wp_query->max_num_pages ) )
            return;
        $pages = $wp_query->max_num_pages;
    }
    else {
        global $$var;
            if( !is_a( $$var, 'WP_Query' ) )
                return;
        if( !isset( $$var->max_num_pages ) || !isset( $$var ) )
            return;
        $pages = absint( $$var->max_num_pages );
    }
    if( $pages < 1 )
        return;
    $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    echo 'Page ' . $page . ' of ' . $pages;
}

NOTE: Code can go into your functions file.

Simply call the function where you want to show the "Page x of y" message, eg.

<?php current_paged(); ?>

If you need the code to work with a custom query, ie. one you've created using WP_Query, then simply pass along the name of the variable that holds the query to the function.

Example non-existant query:

$fred = new WP_Query;
$fred->query();
if( $fred->have_posts() ) 
... etc..

Getting the current page for the custom query using the function posted earlier..

<?php current_paged( 'fred' ); ?>

If you want to just totally forget the custom query support and you're looking for a one-liner, then this should do it..

<?php echo 'Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 ) . ' of ' . $wp_query->max_num_pages; ?>

Hope that helps.. :)

share|improve this answer
1  
If I were you I'd check if $var is a query: if( is_a( $var, 'WP_Query' ) ) will return true if it's a query object. –  John P Bloch Mar 18 '11 at 14:30
    
Yes, that's actually a very sensible idea, done!. –  t31os Apr 16 '11 at 10:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.