Ad

Next_posts_link() Won't Return Anything When Using Pagination With A WP Posts Query

- 1 answer

I've poured over dozens of posts, and the documentation, nothing has helped.

I have a very simple problem: I want to query for all posts of some category, then split up the results into pages. I have tried a few different things, but essentially the next_posts_link() and get_next_posts_link() never returns anything.

Latest code:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
  'posts_per_page' => 6,
  'paged'          => $paged,
  'cat' => 135
);

$query = new WP_Query( $args ); 
    
?>
    
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
<div class="med-item talks-texts">
    <p class="bold"><?php the_title(); ?> (<?php the_time('Y'); ?>)</p>
    <?php the_content(); ?>
</div>
    
    
<?php endwhile; endif; ?>
    
<?php next_posts_link(); ?>
Ad

Answer

Add parameters to the function. In your case:

<?php echo get_next_posts_link('Older', $query->max_num_pages);?>
<?php echo get_previous_posts_link('Newer', $query->max_num_pages);?>
Ad
source: stackoverflow.com
Ad