Snippet: Get the correct page in WordPress

Here is another really quick snippet.
If you have a page where you’re using Query Posts you’re going to have trouble with Pagination.
Wordpress for some reason likes to use page and paged randomly.

If you assign your page as the “front page”, you’re going to have to get_query_var(‘page’), and if you’re going to assign it elsewhere, it’s going to be paged. This is all fine and dandy, at least when you’re in control, but as soon as someone else starts using your theme, you need something more trustworthy, and that’s what this is.

get_current_page() checks the paged variable first, then the page, and if nothing works returns 1, as you’re most probably on page 1.

function get_current_page() {
    if ( get_query_var('paged') ) { 
        return get_query_var('paged'); 
    }
    elseif ( get_query_var('page') ) { 
        return get_query_var('page'); 
    }
    else { 
        return 1; 
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *