WordPRess是强大的,总能满足用户的想法,通过搜索找到了相关的函数代码。
默认直接调用的代码
<?phpprevious_post_link('上一篇: %link')?><?phpnext_post_link('下一篇: %link')?>
当文章处于首篇或末篇时,会显示空白,但可以通过增加判断还填补空白:
<?phpif(get_previous_post()){previous_post_link('上一篇: %link');}else{echo"已是最后文章";}?><?phpif(get_next_post()){next_post_link('下一篇: %link');}else{echo"已是最新文章";}?>
经过测试虽然显示同分类下的文章,但首篇文章和末尾的文章会不显示对应的提示信息“已是最后文章”和“已是最后文章”。只要在get_previous_post()函数中指定一下文章所属分类ID便能使代码完全有效。
下面是完整的代码:
<?php$categories=get_the_category();$categoryIDS=array();foreach($categoriesas$category){array_push($categoryIDS,$category->term_id);}$categoryIDS=implode(",",$categoryIDS);?><?phpif(get_previous_post($categoryIDS)){previous_post_link('上一篇: %link','%title',true);}else{echo"已是最后文章";}?><?phpif(get_next_post($categoryIDS)){next_post_link('上一篇: %link','%title',true);}else{echo"已是最新文章";}?>
打开主题目录下的文章页single.php,在要显示的位置添加代码,保存文件即可。
原文地址:http://www.boke8.net/same-cat-pre-next-post.html