我正在使用此代码,但是一旦我点击下一篇帖子/上一篇帖子链接,我就被重定向到不同类别的下一篇帖子/上一篇文章
previous_post_link('%link', 'Prev post in category', $in_same_term = true);
next_post_link('%link', 'Next post in category', $in_same_term = true);
我正在尝试使用这篇文章http://codex.wordpress.org/Function_Reference/next_post_link
解决我的问题由于
答案 0 :(得分:1)
以下是根据帖子
获取基于类别的上一个和下一个链接的代码$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
}
?>
答案 1 :(得分:0)
你可以这样做......
只需将参数$ in_same_term更改为true ..
previous_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' );