0

On my single.php I would like to show the single post then list all the posts with a pagination.

I use the following code:

<?php while ( have_posts() ) : the_post(); ?>
<!-- Show single post -->
<?php endwhile; ?>

<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$all_posts_query = new WP_Query( array('posts_per_page' => 12, 'paged' => $page));
while($all_posts_query->have_posts()) : $all_posts_query->the_post(); ?>
<!-- Show all the posts -->
<?php endwhile; wp_reset_postdata(); ?>

<?php next_posts_link('older', '', 'yes'); ?>

As result I can see the single post then the first 12 posts and that all, no pager...

Thanks a lot for your time.

1

Pagination does not work well will with secondary queries.

First, the ordinary pagination functions depend upon the main query in the global variable $wp_query. They won't work with secondary queries. There are already a number of questions here about that if you search the site.

Second, trying to paginate secondary queries tends to result in 404 status pages. That is because the query that runs before the template loads-- the "main" query-- is the one that determines what page to load and it does not know about the query written into your template.

I would suggest you use paginate_links for your secondary query pagination. There are numerous examples of use here on this site, for example: https://wordpress.stackexchange.com/a/73928/21376

1
  • Thank for these explanations @s_ha_dum, I understand well. – jjj Jul 28 '13 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.