Fernando Souza
Fernando Souza

Reputation: 1779

Exclude present term from loop

I have foreach loop that prints all the categories from a certain post type. In my taxonomy page, I would like to print all the terms that belong to that taxonomy but not the present term. How can I do it using a for loop?

$presentTerm = get_queried_object();

$terms = get_terms('success-storiescat');
foreach ($terms as $key => $term && $term != $presentTerm) {
  $link = get_term_link($term);
  echo '<option value="'.$link.'">'.$term->name.'</option>';
}

Upvotes: 0

Views: 75

Answers (2)

Simone Rossaini
Simone Rossaini

Reputation: 8162

Put into an array $presentTerm and use array_diff for filter like

$terms= array_diff($value, $exclude);
foreach ($terms as $key => $term) {
$link = get_term_link($term);
echo '<option value="'.$link.'">'.$term->name.'</option>';
}

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57121

You cannot just add the condition to the foreach() you could do it inside the loop...

foreach ($terms as $key => $term) {
    if ($term != $presentTerm) {
        $link = get_term_link($term);
        echo '<option value="'.$link.'">'.$term->name.'</option>';
    }
}

Upvotes: 1

Related Questions