我们在做wordpress网站的时候,需要在位置的尾部增加上下篇文章,提升用户体念。
但是默认的代码,常常会把其他分类的文章给调用出来,特别是把页面调用出来,给用户的体念反而不好。
我们可以通过下面的代码来实现这个按分类来调用上下篇文件的方法:
<!--上下篇开始--> <div class="shangxia"> <?php $current_category=get_the_category(); $prev_post = get_previous_post($current_category,''); $next_post = get_next_post($current_category,'');?> <?php if (!empty( $prev_post )): ?> <a href="<?php echo get_permalink( $prev_post->ID ); ?>" title="上一篇:<?php echo $prev_post->post_title; ?>">上一篇:<?php echo $prev_post->post_title; ?></a> <?php else : ?> <a title="已经是第一篇了">已经是第一篇了</a><?php endif; ?><!--上下片分离--> <?php if (!empty( $next_post )): ?> <a href="<?php echo get_permalink( $next_post->ID ); ?>" title="下一篇:<?php echo $next_post->post_title; ?>">下一篇:<?php echo $next_post->post_title; ?></a> <?php else : ?> <a title="已经是最后一篇了">已经是最后一篇了</a> <?php endif; ?> </div> <!--上下篇结束-->
前端显示的样式,可以根据自己的需要来写。


湘公网安备 43021102000177号