WordPress的循环通过帖子,并检查帖子是否在同一类别
问题描述:
是否有可能在wordpress循环通过帖子,然后如果下一篇文章不在同一类别然后做一些事情。因此,每个岗位的内环路WordPress的循环通过帖子,并检查帖子是否在同一类别
我需要得到cateogry或类别的职位是。
然后一些如何存储和比较,在循环中的下一个职位。
答
我的策略是为以前的类别设置一个校验数组,然后每次比较。
因此,您可以在循环中检查当前类别
例如,
//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();
}
}
我觉得应该为你
你在做什么的比较做呢?就像...你用真假做什么? – rnevius 2014-11-20 16:58:08
@rnevius我正在使用woocommerce,但客户不希望用户能够从两个不同的类别订购产品。例如,他们不会订购假期旅行和一日游,但可以订购多次一日游。因此,如果帖子类别与先前的帖子类别不匹配,则会出现错误消息。 – con322 2014-11-20 17:07:16