默认调用:
<?php previous_post_link('上一篇: %link') ?>
<?php next_post_link('下一篇: %link') ?>
当文章处于首篇或末篇时,会显示空白,但可以通过增加判断还填补空白:
<?php if (get_previous_post()) { previous_post_link('上一篇: %link');} else {echo "已是最后文章";} ?>
<?php if (get_next_post()) { next_post_link('下一篇: %link');} else {echo "已是最新文章";} ?>
经过测试虽然显示同分类下的文章,但首篇文章和末尾的文章会不显示对应的提示信息“已是最后文章”和“已是最后文章”。只要在get_previous_post()函数中指定一下文章所属分类ID便能使代码完全有效。
<?php
$categories = get_the_category();
$categoryIDS = array();
foreach ($categories as $category) {
array_push($categoryIDS, $category->term_id);
}
$categoryIDS = implode(",", $categoryIDS);
?>
<?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "已是最后文章";} ?>
<?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "已是最新文章";} ?>
打开主题目录下的文章页single.php,在要显示的位置添加代码,保存文件即可。
例子:
<div class="inner-page">
<?php
$categories = get_the_category();
$categoryIDS = array();
foreach ($categories as $category) {
array_push($categoryIDS, $category->term_id);
}
$categoryIDS = implode(",", $categoryIDS);
$prev_post = get_previous_post($categoryIDS);
$next_post = get_next_post($categoryIDS);?>
<?php if($next_post){?><a class="iconPic inner-page-up" id="nexturl" title="<?php echo $next_post->post_title;?>" href="<?php echo get_permalink( $next_post );?>"></a><?php }?>
<?php if($prev_post){?><a class="iconPic inner-page-down" id="beforurl" title="<?php echo $prev_post->post_title;?>" href="<?php echo get_permalink( $prev_post );?>"></a><?php }?>
</div>