1

I am using a custom WP_Query to display posts on static front page. and generating numbered pagination links with paginate_links functions. This is the code I have...

$latest_args = array(
    'post_type' => 'post',
    'category__not_in' => array( 3 ),
    'ignore_sticky_posts' => 1,
    'paged' => $paged,
);

$latest_query = new WP_Query( $latest_args );

if ( $latest_query->have_posts() ) :

    while ( $latest_query->have_posts() ) : $latest_query->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;

    if ( $latest_query->max_num_pages > 1 ) :
        $big = 999999999;
        echo '<div class="pagination">';
        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $latest_query->max_num_pages
        ) );
        echo '</div>';
    endif;

endif;

wp_reset_postdata();

But when I use this on my static front page then it always show first page content on all other pages too. But it was corrected by adding paged parameter to query as suggested in codex.

if ( get_query_var('paged') ) {
  $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
  $paged = get_query_var('page');
} else {
  $paged = 1;
}

Which fixed the duplicate content on paginated pages but number based pagination links are always stick to first page. It always displays first link (1) as current which makes difficult to browse rest of the pages.

Although this code works fine if I use it on any other page except static front page.

Any solution to this issue.

EDIT: Update

I have tried disabling all plugins and removed all custom developed functions from functions.php but the problem still exists. So I am guessing this has to do something with how I am using WP_Query or paginate_links function.

There should be some mistake in this code.

3

Static pages doesn't work with paged query variable, they need the page variable.

This is the reason why your second code block makes the query work: it uses the page var when available.

However, your paginated links code always use paged:

...
'format' => '?paged=%#%',
...

So you are sending paged query var but looking for page var.

Solution is to make your format argument send page query var if used from a static page.

if ( $latest_query->max_num_pages > 1 ) :

    $var = is_page() ? 'page' : 'paged'; // <-- choose var here   

    $big = 999999999;
    echo '<div class="pagination">';
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

        'format' => '?' . $var . '=%#%', // <-- use var here  

        'current' => max( 1, get_query_var($var) ), // <-- use var here 
        'total' => $latest_query->max_num_pages
    ) );
    echo '</div>';
endif;

Untested.

  • 1
    I think you are correct on the format part. I can remember having to do this once but wasn't 100% sure about that. But on top of my head, you are correct – Pieter Goosen Nov 18 '14 at 10:31
  • How silly! I totally missed it. This works but I also had to change 'current' => max( 1, get_query_var('paged') ) too. Thanks G.M. – Robert hue Nov 18 '14 at 10:44
  • 1
    @Roberthue Glad it worked. I updated the answer to use $var in for "current" argument too. – gmazzap Nov 18 '14 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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