WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query and prev/next links (2 posts)

  1. 10010110
    Member
    Posted 1 year ago #

    I’m displaying the content of a few pages (not posts!) on a page using WP_Query; I have limited the amount of items to display and now I’m wondering how to add “next” and “prev” links to it. What I have isn’t working:

    $query = new WP_Query(array(
    	'post_type' => 'page',
    	'posts_per_page' => 2,
    	'paged' => get_query_var('page')
    ));
    if($query -> have_posts()):
    	while($query->have_posts()) : $query->the_post();
    ?>
    // stuff here
    	<?php next_posts_link(); ?>
    	<?php previous_posts_link(); ?>
    	<?php
    		endwhile;
    		wp_reset_postdata();
    	endif;
    ?>

    What am I doing wrong? (P. S.: I do have more than two pages)

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I believe that next and previous_posts_links() only work with $wp_query. Try saving the current query and using query_posts() instead of new WP_Query().

Topic Closed

This topic has been closed to new replies.

About this Topic