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'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?

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(); ?>
share|improve this question
add comment

1 Answer 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;
?>
share|improve this answer
add comment

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.