pagination elements not displaying in WordPress loop

I have the following code:

$attr = array(
        'align' => 'left',
        'class' => 'thumbnail imageRight',
        'width' => 350,
        'height' => 350
);

$post_query =  array ( 'post_type' => 'post' );
$posts = new WP_Query ( $post_query );

if($posts->have_posts()){
    while($posts->have_posts()){
        $posts->the_post();
        ?>
        <div class="post">
            <?php the_post_thumbnail('medium', $attr); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <p><?php the_excerpt(); ?></p>
        </div>
        <?php
    }

    next_posts_link('&laquo; Older Entries');
    previous_posts_link('Newer Entries &raquo;');
}

Which can be seen in action here. This page displays currently 3 of the 21 posts in the database. How ever there is no pagination.

Read More

Can some one tell me why?

Related posts

Leave a Reply

2 comments

  1. Both next_posts_link and previous_posts_link use the global $wp_query and $paged. You have to chase function calls around the source code to see that (but it is pretty obvious with next_post_links). They don’t work with custom queries but I believe you can cheat.

    $old_wpq = $wp_query;
    $wp_query = new WP_Query ( $post_query );
    // your loop
    $wp_query = $old_wpq;
    

    Try that.

    There is a related thread wordpress.stackexchange.com.

    https://wordpress.stackexchange.com/questions/77661/next-posts-link-works-only-with-original-wp-query/77666#77666

  2. <?php
    global $wp_query;
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array(  
                'post_type' => 'post', //Post type
                'posts_per_page' => 3, //How many post u want to display per page
                'paged' => $paged                      
                );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
        $the_query->the_post();
    
        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    ?>
        <img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <p><?php the_excerpt(); ?></p>
    
    <?php } } ?>
    <div class="pagination">
    <?php             
        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
        ) );
    ?>
    </div>