0

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?

3

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 ?>

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.