In WordPress, I have a page for News that is using a specific page template I created. This page is populated by news posts assigned to the news category. I need to show only two of the latest news posts on this page with Previous / Next links at the bottom if users want to read more news. However, the next / previous links are not working. Clicking on them does go to /page/2 but it’s shows the same posts. Here is my code .. any ideas / help appreciated!
<div id="primary">
<div id="content" role="main">
<div id="news">
<?php query_posts( "category_name=news&orderby=date&order=ASC&posts_per_page=2" ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<p class="post-date">Posted on <?php the_date(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
<div id="more-posts">
<div class="previous-link"><?php previous_posts_link("< previous news") ?></div><div class="next-link"><?php next_posts_link ("more news >") ?></div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
When you perform a custom query such as this one, you have to do a bit of a “hack” to get pagination to work properly.
Change this:
…to a
WP_Query
call:And then you need to *move your custom query object into the
$wp_query
global`:Then, update your loop initiation, from this:
…to this:
This should cause the pagination to work as expected.
When you close your loop:
…just restore the original
$wp_query
global:And you should be good to go.