2014-11-20 93 views
0

是否有可能在wordpress循環通過帖子,然後如果下一篇文章不在同一類別然後做一些事情。因此,每個崗位的內環路WordPress的循環通過帖子,並檢查帖子是否在同一類別

我需要得到cateogry或類別的職位是。

然後一些如何存儲和比較,在循環中的下一個職位。

+1

你在做什麼的比較做呢?就像...你用真假做什麼? – rnevius 2014-11-20 16:58:08

+0

@rnevius我正在使用woocommerce,但客戶不希望用戶能夠從兩個不同的類別訂購產品。例如,他們不會訂購假期旅行和一日遊,但可以訂購多次一日遊。因此,如果帖子類別與先前的帖子類別不匹配,則會出現錯誤消息。 – con322 2014-11-20 17:07:16

回答

0

我的策略是爲以前的類別設置一個校驗數組,然後每次比較。

因此,您可以在循環中檢查當前類別

例如,

//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(); 
    } 

} 

我覺得應該爲你

相關問題