0

I can't seem to display previous and older set of posts links. This is the page I am working on. I need a link to older and newer posts at the bottom of page.

The template code is like this:

<?php /*
Template Name: artikkelit
*/ ?>

<?php get_header(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php $the_query = new WP_Query( 'showposts=5' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<div class="postPic"> <?php the_post_thumbnail(); ?></div>

<?php the_excerpt(); ?>

<?php endwhile;?>

</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>

Any suggestions on solving this?

1 Answer 1

1

The other question is completely wrong and should not be used

  • previous_post() and next_post() is both depreciated functions and should not be used anymore

  • Never replace WP_Query with query_posts to solve a problem. This actually creates more. Also, query_posts should never be used

  • showposts is also depreciated in favor of posts_per_page

To make pagination work, you will need to do the following instead

  • Set the paged parameter in your query arguments (paged=$paged)

    $paged = (get_query_var('paged')) ?get_query_var('paged') : 1;
    
  • Use previous_posts_link() and next_posts_link() to paginate your posts

  • Set the $max_num_pages parameter for next_posts_link() as to correctly calculate the amount of pages when using a custom query

Your query should look something like this. (Taken from the codex)

<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$the_query = new WP_Query( 'posts_per_page=5&paged=' . $paged ); 
?>

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

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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