2013-04-20 33 views
0

在單CPT.php下,我有一個select標籤,它顯示了我的分類術語。我首先返回當前職位的條款,然後返回其他條款。問題在於,即使在執行if測試之前,我也有冗餘的當前期限,但在顯示其他條款之前,if測試排除了當前帖子的期限。我的代碼在下面有什麼錯誤?你的幫助是有價值的。排除分類術語表中當前職位的條款

<select class="select"> 
             <?php 

              $my_current_term = wp_get_object_terms(get_the_ID(), 'product_category'); 
              foreach($my_current_term as $c_term) 
              { 
              ?> 


              <option value="<?php echo $c_term->name;?>"> 
              <?php echo $c_term->name; ?> 
              </option> 
              <?php 

              } 

              $all_my_terms = get_terms('product_category');//add asc and order by name in asc 



              foreach ($all_my_terms as $term1) { 
               if ($my_current_term->name != $term1->name) { 

               $option = '<option value="'.$term1->name.'">'; 
               $option .= $term1->name; 
               $option .= '</option>'; 
               echo $option; 
               } 
              } 

              ?> 

             </select> 

回答

1

您可以排除條款,同時讓他們:

$all_my_terms = get_terms('product_category', array('exclude' => $c_term->term_id));

此外,使用wp_get_object_terms時,一定要通過設置fields參數ids獲得長期ID數組:

wp_get_object_terms(get_the_ID(), 'product_category', array('fields' => 'ids'));