Ask your WordPress questions! Pay money and get answers fast! (more info)

Pagination on Template Page WordPress

  • SOLVED

I have a template page used for displaying the posts. When I click on the "next posts" button it goes to page 2 but loads the same posts.

Here is my code on my template page:

$wp_query = new WP_Query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query->query( array( 'posts_per_page' => '6', 'paged' => $paged ) );
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

... Do Stuff

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="post-nav>">
<div class="nav-previous"><?php next_posts_link('<span class="meta-nav">?</span> Next'); ?></div>
<div class="nav-next"><?php previous_posts_link('Previous <span class="meta-nav">?</span>'); ?></div>
</nav>

Answers (4)

2012-11-02

Arnav Joy answers:

try this above $wp_query = new WP_Query(); as

wp_reset_query();

$wp_query = new WP_Query();


Arnav Joy comments:

try this

<?php wp_reset_query();

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts( array( 'posts_per_page' => '6', 'paged' => $paged ) );

while (have_posts()) : the_post(); ?>



... Do Stuff



<?php endwhile; ?>



<nav id="post-nav>">

<div class="nav-previous"><?php next_posts_link('<span class="meta-nav">?</span> Next'); ?></div>

<div class="nav-next"><?php previous_posts_link('Previous <span class="meta-nav">?</span>'); ?></div>

</nav>


Anthony Moore comments:

That seemed to work.

Any reason why pagination wasn't working with WP_Query vs query_posts?


Arnav Joy comments:

Well there is not definite reason for it , some time WP_Query not gets overwrite and takes default value so query_posts works

2012-11-02

John Cotton answers:

Have you tried

<blockquote>$paged = (get_query_var('page')) ? get_query_var('page') : 1;</blockquote>

?


Anthony Moore comments:

Yes unfortunately that does not work.

2012-11-02

Baki Goxhaj answers:

Move this under the pagination lines:

<?php wp_reset_postdata(); ?>


Anthony Moore comments:

Unfortunately that does not work.

2012-11-02

Martin Pham answers:

Please try this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => '6',
'paged' => $paged
);
$wp_query = new WP_Query($args);

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>


... Do Stuff



<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

<nav id="post-nav>">

<div class="nav-previous"><?php next_posts_link('<span class="meta-nav">?</span> Next'); ?></div>

<div class="nav-next"><?php previous_posts_link('Previous <span class="meta-nav">?</span>'); ?></div>

</nav>


Martin Pham comments:


<?php
$args = array(
'posts_per_page' => '6',
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// Do Stuff
endwhile;
?>
<!-- Post Pagination -->
<nav id="post-nav>">
<div class="nav-previous"><?php next_posts_link('<span class="meta-nav">?</span> Next'); ?></div>
<div class="nav-next"><?php previous_posts_link('Previous <span class="meta-nav">?</span>'); ?></div>
</nav>
<?php
endif;
wp_reset_query();