wordpress收费下载资源主题
wordpress付费下载资源插件

wordpress 文章页调用同分类上/下一篇文章

默认调用:

<?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>

0 个评论

定制开发
本站承接WordPress等系统建站仿站、二次开发、主题插件定制等开发服务
在线咨询
  • 请直接说明需求,勿问在否
    QQ:1-247-246-247

  • QQ一群:104228692(满)
  • QQ二群:64786792
在线咨询
本站承接WordPress建站仿站、二次开发、主题插件定制等PHP开发服务!

了解详情