Reputation: 5067
Under single-CPT.php, I have a select
tag which displays terms of my taxonomy. I first return the term of the current post then return the other terms. The problem is that I have a redundant current post term even I perfrom an if
test before displaying the other terms, the if test excludes the term of the current post. What is wrong in my code below? your help is valuable.
<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>
Upvotes: 0
Views: 2000
Reputation: 6828
You can exclude terms while getting them:
$all_my_terms = get_terms( 'product_category', array( 'exclude' => $c_term->term_id ) );
Also, when using wp_get_object_terms
, make sure you get an array of term IDs by setting the fields
argument to ids
:
wp_get_object_terms( get_the_ID(), 'product_category', array( 'fields' => 'ids' ) );
Upvotes: 1