posts_nav_link() not displaying anything

I’m having difficulty getting posts_nav_link() to display anything. Below is my index.php template file, I’m using permalinks (index.php/2011/08/26/sample-post/ format) and I’ve currently got my Blog pages show at most set to -1.

Does anyone know why the links aren’t working?

Read More

index.php

<?php
    get_header();
    $args = array('posts_per_page' => 4,'paged' => get_query_var('page'));
    $myquery = new WP_Query( $args );
?>

    <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>

        <h1><?php the_title(); ?></h1>

        <?php the_content()?>

    <?php endwhile ?>

    <?php posts_nav_link() ?>

    <?php wp_reset_postdata(); ?>

<?php get_footer(); ?>

Related posts

Leave a Reply

1 comment

  1. You need to do a little “hack” to get pagination to work for your custom loop.

    After you define $myquery, do the following:

    <?php
    // globalize $wp_query
    global $wp_query;
    // copy $wp_query into a temporary variable
    $temp_wp_query = $wp_query;
    // nullify $wp_query
    $wp_query = null;
    // move $myquery into $wp_query
    $wp_query = $myquery;
    ?>
    

    At this point, your posts_nav_link() should work as expected.

    Now, after the loop, swap the original object back into $wp_query, so that everything else on the page that is query-dependent will work properly:

    <?php
    // restore original $wp_query
    $wp_query = $temp_wp_query;
    ?>