WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Template Pagination WP_Query not working (9 posts)

  1. afang22
    Member
    Posted 4 years ago #

    Hello,

    I have created a template for a page to display only posts from a specific category. Everything works except for the pagination. I have tried all the suggestions posted here and it still doesn't work.

    Please help!

    Here is my code:

    <?php
    $cat = array(228);
    $do_not_show_stickies = 1; // 0 to show stickies
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args=array(
    'category__in' => $cat,
    'caller_get_posts' => $do_not_show_stickies,
    'paged' => $paged,
    'posts_per_page' => 10,
    );

    $temp = $my_query;
    $my_query= null;
    $my_query = new WP_Query($args);
    ?>

    <?php if( $my_query->have_posts() ) : ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    ....

    <?php endwhile; ?>
    <?php $my_query = null; $my_query = $temp;?>

    <!-- Previous/Next page navigation -->
    <?php next_posts_link('« Older Entries') ?>
    <?php previous_posts_link('Newer Entries »') ?>

    <?php else : ?>
    <h2>Not Found</h2>
    <?php endif; ?>

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    the only 'odd' thing is that you reset the query before the next/prev posts links.

    <?php endwhile; ?>
    <?php $my_query = null; $my_query = $temp;?>
    
    <!-- Previous/Next page navigation -->
    <?php next_posts_link('« Older Entries') ?>
    <?php previous_posts_link('Newer Entries »') ?>

    i would try to do it after the endif;

  3. afang22
    Member
    Posted 4 years ago #

    thanks. i tried moving it but it didn't help. the only reason why i even included that $temp variable was because of another post i read.

    the problem is, the pagination buttons don't even show up. it's almost as it doesn't know there are more posts to be displayed.

  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    have you got a link?

    you could also look into the html source in your browser to see if the html comment <!-- Previous/Next page navigation --> shows.
    you would know then, that the loop finished.

  5. afang22
    Member
    Posted 4 years ago #

    yes, the <!-- Previous/Next page navigation --> shows in the html source.

    the only thing i can think of is that i do have the page navigation plugin installed, but i am not calling the plugin function on my template.

    i'm using the default pagination, which i tested to make sure it works on the archive page.

    <?php next_posts_link('« Older Entries') ?>
    <?php previous_posts_link('Newer Entries »') ?>

  6. afang22
    Member
    Posted 4 years ago #

    does it have anything to do with my WP_Query call. Should i be using query_posts instead?

  7. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    i can't comment on this - so far, i have always used query_posts.

    did you try to deactivate the plugin, or call the plugin postnav function?

    in extreme, try deactivating all plugins, to see if that solves the problem.
    then reactivate them one-by-one, and see if the problem reoccurs.

  8. afang22
    Member
    Posted 4 years ago #

    ah, i have identified the problem.

    i am using my own $my_query instead of the global $wp_query

    pagination only recognizes $wp_query

  9. afang22
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic