【问题标题】:wordpress previous post within category issuewordpress 类别问题中的上一篇文章
【发布时间】:2015-02-20 19:21:45
【问题描述】:

我发现这段代码在同一类别中显示下一个和上一个帖子链接,问题如下:

我发现此代码可以显示同一类别中帖子的 NEXT 和 PREVIOUS 链接。

我有 20 个帖子。如果我从 20 日中的 1 日开始浏览,一切正常。 “上一个”和“下一个”链接完美运行。

但是,如果我单击第 15 号帖子:出现问题,上一个链接会消失,并且下一个帖子链接而不是链接到第 16 号帖子(真正的下一个)链接到该类别的第一个帖子。

您能建议我的代码有什么问题吗?我用它single-loop.php的主题twentyten

$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
}
?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用

    http://codex.wordpress.org/Function_Reference/previous_post_link http://codex.wordpress.org/Function_Reference/next_post_link

    您可以提供类别(术语)和分类(类别)以显示上一个或下一个帖子;

    <?php next_post_link( '%link', 'Next post in category', TRUE ); ?>
    

    【讨论】:

    • 谢谢!!我在尝试一些不同的东西,基本上我需要当你在某个类别“最后一篇”时没有“下一篇”,这个功能可以吗?
    • 我提出了一个新问题,最好问:stackoverflow.com/questions/28630055/…
    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多