admin管理员组文章数量:1335580
I have added the Next and previous post link in a single.php file, I need to show the same categories post in the next and previous button. but it is showing error at first and last point
"Notice: Undefined offset: 5 in C:\xampp\htdocs\build-plugin\wp-content\themes\genesis\single.php on line 28" & "Notice: Undefined offset: -1 in C:\xampp\htdocs\build-plugin\wp-content\themes\genesis\single.php on line 29"
Shows error in this two lines
$prev_post = $ids[$index+1];
$next_post = $ids[$index-1];
Here is my code for next and previous button
$post_id = $post->ID; // Get current post ID
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID; // Get current Category ID
$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
$posts = get_posts($args);
// Get IDs of posts retrieved by get_posts function
$ids = array();
foreach ($posts as $thepost) {
$ids[] = $thepost->ID;
}
// Get and Echo the Previous and Next post link within same Category
$index = array_search($post->ID, $ids);
$prev_post = $ids[$index+1];
$next_post = $ids[$index-1];
?>
<?php if (!empty($prev_post)){ ?> <a class="previous-post" rel="prev" href="<?php echo get_permalink($prev_post) ?>"> <span class="meta-icon"><i class="fa fa-angle-left fa-lg"></i></span> Previous</a> <?php } ?>
<?php if (!empty($next_post)){ ?> <a class="next-post" rel="next" href="<?php echo get_permalink($next_post) ?>">Next <span class="meta-icon"><i class="fa fa-angle-right fa-lg"></i></span> </a> <?php } ?>
本文标签: theme developmentNext and previous post link shows error in first and last post
版权声明:本文标题:theme development - Next and previous post link shows error in first and last post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289623a2447555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论