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.

I am using the below code for pagination. However, it is not working. It's showing all of the pages numbers nicely, but when I click on a page number the first page is always displayed.

global $wp_query;  
$total_pages = $wp_query->max_num_pages;  
if ( $total_pages > 1 ) {  
    $current_page = max( 1, get_query_var( 'paged' ) );  
    echo paginate_links( array(  
        'base' => get_pagenum_link( 1 ) . '%_%',  
        'format' => '/page/%#%',  
        'current' => $current_page,  
        'total' => $total_pages,  
    ) );  
}

My permalink settings are set to:

localhost/my-blog/sample-post/

And query post:

$args = array( 'post_type' => 'post', 'posts_per_page' => 2);

If i change the format to

'/page=%#%'

its providing me 404 error.

anybody please give me idea what to do next for working this pagination.

share|improve this question

1 Answer 1

up vote 1 down vote accepted
check below code for pagination.

global $paged;
$temp = $wp_query;
$wp_query = null;

$wp_query = new WP_Query( array('post_type' => 'post','posts_per_page' => 2, 'paged' => $paged ) ); 
while (have_posts()) : the_post(); 
// Your post content.
endwhile;
echo paginate_links( $args );
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'prev_text'    => __('Previous'),
'next_text'    => __('Next')
) );
/* Code Ends

Check this code and let me know.
share|improve this answer
    
thanks @Arvind Pal –  strange man Jan 1 '13 at 8:39
    
use pre_get_posts to modify the main query rather than creating a new query in the template. –  Milo Jan 1 '13 at 18:20

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.