默认调用:
<?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="https://www.mobantu.com/<?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="https://www.mobantu.com/<?php echo get_permalink( $prev_post );?>"></a><?php }?> </div>
原文链接:https://www.dqzy.cn/2025/05/19/656.html,转载请注明出处。
1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。
4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
评论0