您可以使用 模拟它WP_Query
,因为它包含max_num_pages
属性。如果$paged
等于 1,我们不打印previous
链接。如果它等于max_num_pages
,我们不打印next
链接。
链接是根据get_the_permalink()
我们在执行循环之前抓取的内容构建的。您必须调整您的永久链接结构,检查代码注释。
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$this_page = get_permalink();
$previous = $paged - 1;
$next = $paged + 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
if( $paged != 1 )
{
// DEFAULT PERMALINKS
# echo "<a href='$this_page&paged=$previous'>previous</a>";
// PRETTY PERMALINKS
echo "<a href='{$this_page}page/$previous/'>previous</a>";
}
if( $paged != 1 && $paged != $the_query->max_num_pages )
{
// SEPARATOR
echo ' | ';
}
if( $paged != $the_query->max_num_pages )
{
// DEFAULT PERMALINKS
# echo "<a href='$this_page&paged=$next'>next</a>";
// PRETTY PERMALINKS
echo "<a href='{$this_page}page/$next/'>next</a>";
}
endif;
找到这篇文章,Next/Previous Post Navigation Outside the WordPress Loop,虽然没有帮助,但我将留在这里作为参考。