我发现这个代码在同一类别中显示下一个和上一个帖子链接,问题如下:
我找到了这个代码来显示同一类别中帖子的NEXT和PREVIOUS链接。
我有20个职位。如果我从20个中的1号开始浏览,一切都会好起来。"上一个"answers"下一个"链接完美工作。
但如果我点击第15个帖子:问题出现了,上一个链接消失了,下一个帖子链接而不是链接到第16个帖子(真正的下一个)链接到该类别的第一个帖子。
你能告诉我我的代码出了什么问题吗?我使用主题为twentyten的singleloop.php。
$post_id = $post->ID; // current post id
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID; // current category Id
$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
$posts = get_posts($args);
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($posts as $thepost) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if (!empty($previd)){
?>
<a rel="prev" href="<?php echo get_permalink($previd) ?>">Previous</a>
<?php
}
if (!empty($nextid)){
?>
<a rel="next" href="<?php echo get_permalink($nextid) ?>">Next</a>
<?php
}
?>
使用
http://codex.wordpress.org/Function_Reference/previous_post_linkhttp://codex.wordpress.org/Function_Reference/next_post_link
您可以提供类别(术语)和分类法(类别)来显示上一篇或下一篇文章;
<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>