I am using a Specialized Page Template to display list of posts. I am using the following code for that:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged
);
$all_posts = get_posts($args);
?>
<?php foreach ( $all_posts as $post ) : setup_postdata( $post ); ?>
/* the loop */
<?php endforeach; ?>
Now I want to put “Newer Posts” and “Older Posts” links below it. next_posts_link()
and previous_posts_link()
prints nothing here. How can I add these two links on this page?
Use the below code
As from the codex
next_posts_link()
andprevious_posts_link()
doesn’t work in the custom page template as a static pageRefer codex for more details
These functions does not work with static pages
You can simulate it using
WP_Query
, as it contains themax_num_pages
attribute. If$paged
is equal to 1, we don’t print theprevious
link. If it is equal tomax_num_pages
, we don’t print thenext
link.The links are constructed based on
get_the_permalink()
that we grab before doing the loop. You have to adjust to your permalinks structure, check the code comments.Found this article, Next/Previous Post Navigation Outside of the WordPress Loop, although it didn’t help, I’ll leave here as reference.