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(); ?>
You need to do a little “hack” to get pagination to work for your custom loop.
After you define
$myquery
, do the following: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: