是否可以在wordpress中循环播放帖子,然后如果下一篇文章不属于同一类别,则可以执行某些操作。
因此,在每个帖子的循环中,我需要获得帖子所在的类别或类别。
然后一些如何存储并将其与循环中的下一篇文章进行比较。
答案 0 :(得分:0)
我的策略是为之前的类别设置一个检查数组,然后每次比较。
因此,在循环内部,您可以检查当前类别
e.g。
//set a check array
$previous_categories = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
// get the categories for this post
$current_categories = get_the_category();
// set up a temp array to change the previous_categories array when we have finished
$tmp_categories = array();
// now iterate through the categories and see if it is in the previous post
if($categories){
foreach($categories as $category) {
// do the check
if (in_array($category->term_id,$previous_categories)){
// then its in the previous and you do your thing
}
// add the current id to the temp array
$tmp_categories[] = $category->term_id;
}
// now we can set for the next post
$previous_categories = $tmp_categories;
} else {
// there were no categories for this post so it cant match
// but we do need to initialise the previous_categories array
$previous_categories = array();
}
}
我认为应该为你做这件事