doufei2328 2019-05-10 06:02
浏览 52
已采纳

如何在wp_query循环中获取下一篇文章的类别

I need to check if current post in the loop has the same category that the next one.

while($query->have_posts()){
    $query->the_post();
        if(category_of_this_post == category_of_the_next_post) //this
        //do something
    }
  • 写回答

2条回答 默认 最新

  • dqrnsg6439 2019-05-10 07:15
    关注

    A rough setup is the following.

    while ( $query->have_posts() ) : $query->the_post();
    
        $next                 = $query->posts[ $query->current_post + 1];
        $current_categories   = get_the_category();
        $next_post_categories = get_the_category($next->ID);
    
        if ( /* compare $current_categories  $next_post_categories */ ) {
    
        }
    endwhile;
    wp_reset_postdata(); // always reset
    

    A few things to keep in mind:

    • You can assign multiple categories, so think about how you want to compare
    • On the last post you will get an notice. because it does not exist. Build a check for that.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决