排除分类法术语列表中当前帖子的术语

时间:2013-04-20 13:35:31

标签: php wordpress taxonomy custom-post-type term

在单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 个答案:

答案 0 :(得分: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' ) );