WP 显示下一遍或者上一遍文章
1,按时间排序显示上一遍或者下一遍文章
<div>
<?php if (get_next_post()) { next_post_link(‘Next : %link’);} else {echo “Next : This is the first post !”;} ?>
</div>
<div>
<?php if (get_previous_post()) { previous_post_link(‘Privious : %link’);} else {echo “Privious : This is the last post !”;} ?>
</div>
2,按分类排序显示上一遍或者下一遍文章
<?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 “没有了,已经是最新文章”;} ?>
其中函数
- <?php previous_post_link($format, $link, $in_same_cat = false, $excluded_categories = ”); ?>
- <?php next_post_link($format, $in_same_cat = false, $excluded_categories = ”); ?>
相关说明:
$format:格式化被显示的字符串,缺省值是”‘« %link”,第二个函数缺省值是”%link »”。
$link:被显示的字符串,缺省值是上一篇或下一篇的”$title”,也可以设置为其它你想显示的字符串。
$in_same_cat :表示是显式同一类别下的文章还是不区分类别的文章,缺省值false表示不区分类别,只以发帖的时间先后来确定。
$excluded_categories:表示在显示上一篇或下一篇时是否排除掉某分类,缺省不排除,如果排除,把分类ID列在此处,以英文逗号分隔。
Leave a Reply