Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add next and previous page buttons to my template, but when I try to use either next_post_link or previous_post_link, it just does not work.

Could it be because I am using WP_Query?

Here is my code thus far,

<?php get_header(); ?>

            <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
            <?php $args = array(
                "post_type" => "page", 
                "page_id" => $post->ID, 
                ); ?>

            <?php $query = new WP_Query($args); ?>

            <?php if ($query->have_posts()) : ?>

                <?php while ($query->have_posts()) : $query->the_post(); ?>
                    <!-- do stuff -->
                <?php endwhile; ?>

                    <div id="footer_nav_container">
                        <div class="left"><?php previous_post_link(); ?></div>
                        <div class="right"><?php next_post_link(); ?></div>
                    </div>

            <?php else: ?>
                <!-- do other stuff here -->                
            <?php endif; ?>

<?php get_footer(); ?>

Am I maybe missing essential parameters? From what I can see in the codex, it should work fine as I am using it now?

share|improve this question
add comment

1 Answer

next_post_link & previous_post_link work off the global $wp_query. You could simply overwrite the main query with $wp_query =& $query, or replace your custom query with the standard 'global' functions.

<?php query_posts( array( "post_type" => "page", "page_id" => $post->ID ) ) ?>
<?php if ( have_posts() ) : ?>

    <?php while ( have_posts() ) : the_post() ?>
        <!-- do stuff -->
    <?php endwhile ?>

    <div id="footer_nav_container">
        <div class="left"><?php previous_post_link(); ?></div>
        <div class="right"><?php next_post_link(); ?></div>
    </div>

<?php else : ?>

    <!-- do other stuff here -->

<?php endif ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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