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.

How do I determine if I'm on the very first page of pagination? I'm using WP_Pagenavi. I want to run a function only on the first page of the pagination. I checked the query_var 'paged', it's set to 0 on this page, and then 2, 3 and so on in the later pages (1 is missing!)... Anyone knows a clean solution?

Thanks.

share|improve this question

3 Answers 3

up vote 7 down vote accepted
// get current page we are on. If not set we can assume we are on page 1.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
if(1 == $paged) {
    //true
}
share|improve this answer
    
Thanks! This is what I always use for my other custom loops. I tried printing the query_var 'paged', it is set to 0 on the first page. Hence I was confused and thought I should be sure! Accepted. –  Rutwick Gangurde Oct 14 '11 at 8:30

if you only want to know that you are on the first page of a paginated page, try:

if( !is_paged() ) {
  //first page of pagination
}

http://codex.wordpress.org/Function_Reference/is_paged

share|improve this answer

I was looking for a simple way to determine whether or not to use the posts_nav_link() function and all solutions I found online were either too complex or unreliable. For example, many people suggested using the $paged global variable, but I found that this variable returned the same value for the first page, even when the first page was the only page!

So, I dug into the wp-includes/link-template.php file, and found that the posts_nav_link() function simply outputs the return value of another function:

/**
 * Display post pages link navigation for previous and next pages.
 *
 * @since 0.71
 *
 * @param string $sep Optional. Separator for posts navigation links.
 * @param string $prelabel Optional. Label for previous pages.
 * @param string $nxtlabel Optional Label for next pages.
 */
function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
    $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
    echo get_posts_nav_link($args);
}

Using this knowledge, we can create a simple and effective way to determine whether or not we need to add links to navigate between pages:

$posts_nav = get_posts_nav_link();
if(empty($posts_nav)) {
    // do not use posts_nav_link()
} else {
    // use posts_nav_link()
}

Originally posted on my blog here: http://igalvez.net/wordpress/checking-when-to-add-links-between-pages/

Hope this helps!

share|improve this answer
2  
I appreciate your concern as you answered a very old question and one that has already been answered! +1 :) –  Rutwick Gangurde Mar 19 '13 at 4:46

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.