Support » Fixing WordPress » Query Posts and Pagination

  • Resolved coopersita



    I’m having problems with my pagination. When in my home page (static page), I click on the Next Page link, it takes me to page 2, but in page 2, there is not Previous Page, and the Next Page points to page 2, again.

    Here’s what I have around my loop:

    <?php $page = (get_query_var('page')) ? get_query_var('page') : 1;
    	query_posts("post_type=myposttype&paged=$page"); ?>
    <?php while (have_posts()) : the_post(); ?>
         ...My posts here
    <?php endwhile; ?>
    <?php next_posts_link('Next Page &raquo;') ?>
    <?php previous_posts_link('&laquo; Previous Page') ?>

    I have tried adding wp_reset_query(); before and after the loop, but before it breaks it, and after it doesn’t seem to make a difference.

    I have some more loops in the page (on the sidebar, and one that only shows up in the first page). Could that affect it?

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi

    The first line of your code is incorrect.

    Instead of this
    <?php $page = (get_query_var('page')) ? get_query_var('page') : 1;
    it should be this
    <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;

    That should get it working

    Actually, since it’s a page, “get_query_var(‘page’)” works, while “get_query_var(‘paged’)” doesn’t. With “get_query_var(‘paged’)”, all my pages started with post #1, instead of keeping count.

    I since changed it into custom query:

    $page = (get_query_var('page')) ? get_query_var('page') : 1;
    $wp_query = new WP_Query(array( 'post_type' => 'tips', 'posts_per_page' => 10, 'paged' => $page )); ?>
    <?php 	while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    I ended up making my own previous and next links, since that’s the only thing that wasn’t working (and for some reason, in the navigation I needed the “get_query_var(‘paged’)”):

    $pages = intval(ceil($wp_query->found_posts / $posts_per_page));
    if(get_query_var('paged') < $pages):
    	<div class="left"><a href="/page/<?php echo get_query_var('paged') + 1; ?>">« Older Tips</a></div>
    <?php  endif;
    if(get_query_var('paged') != 1): ?>
    	<div class="right"><a href="/page/<?php echo get_query_var('paged') - 1; ?>">Newer Tips »</a></div>
    <?php endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query Posts and Pagination’ is closed to new replies.