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

我需要检查循环中的当前帖子是否与下一个帖子具有相同的类别。


while($query->have_posts()){

    $query->the_post();

        if(category_of_this_post == category_of_the_next_post) //this

        //do something

    }


拉莫斯之舞
浏览 136回答 2
2回答

慕田峪9158850

粗略的设置如下。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需要牢记以下几点:您可以分配多个类别,因此请考虑如何比较在最后一个帖子中,您将收到通知。因为它不存在。建立一个检查。

ABOUTYOU

这在过去对我有用,可以将上一个和下一个保持在同一只猫中:&nbsp;<?php&nbsp;previous_post_link(&nbsp;'%link',&nbsp;__(&nbsp;'<span&nbsp;class="prev"><<&nbsp;Previous&nbsp;Post</span>',&nbsp;'esquire'&nbsp;),&nbsp;TRUE&nbsp;);&nbsp;?> &nbsp;<?php&nbsp;next_post_link(&nbsp;'%link',&nbsp;__(&nbsp;'<span&nbsp;class="prev">Next&nbsp;Post&nbsp;>></span>',&nbsp;'esquire'&nbsp;),&nbsp;TRUE&nbsp;);&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP