从自定义分类法中删除 foreach 循环中的重复项

Remove duplicates in foreach loop from custom taxonomy

它在 foreach 循环中显示了这个结果,它应该删除任何重复项

spain
philippines
spain

我尝试使用 array_unique() 但它对我不起作用

这是我正在使用并且工作正常的代码,只是它有重复项

<?php 
if (is_tax() || is_category() || is_tag() ){
    $qobj = get_queried_object();

    $args = array(
      'posts_per_page' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => $qobj->taxonomy,
          'field' => 'slug', 
          'terms' => $qobj->slug
        )
      )
    );

    $random_query = new WP_Query( $args );

    if ($random_query->have_posts()) {
        while ($random_query->have_posts()) {
          $random_query->the_post();
            $term_list = wp_get_post_terms($post->ID, 'country', array("all"));
            foreach($term_list as $term_single) {
                echo '<a href="'.dirname(get_the_permalink()).'">';
                echo $term_single->slug;
                echo '</a><br>';
            }
        }
    }
    wp_reset_query();
} 
?>

添加这个用于调试 - var_dump($term_list);这是结果

array (size=1)
  0 => 
    object(WP_Term)[1464]
      public 'term_id' => int 2
      public 'name' => string 'Spain' (length=5)
      public 'slug' => string 'spain' (length=5)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 2
      public 'taxonomy' => string 'country' (length=7)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 2
      public 'filter' => string 'raw' (length=3)

array (size=1)
  0 => 
    object(WP_Term)[1473]
      public 'term_id' => int 5
      public 'name' => string 'Philippines' (length=11)
      public 'slug' => string 'philippines' (length=11)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 5
      public 'taxonomy' => string 'country' (length=7)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 2
      public 'filter' => string 'raw' (length=3)

array (size=1)
  0 => 
    object(WP_Term)[1475]
      public 'term_id' => int 2
      public 'name' => string 'Spain' (length=5)
      public 'slug' => string 'spain' (length=5)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 2
      public 'taxonomy' => string 'country' (length=7)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 2
      public 'filter' => string 'raw' (length=3)    

我只需要删除重复项就可以了,我对此不是很熟悉而且它非常复杂,从昨天开始我就在努力解决这个问题,任何人都请帮助我让它工作或者只是在正确的轨道上,谢谢

如果您保留已显示的列表,则在显示新列表之前检查它是否在该列表中(使用 in_array())。显示后,将其添加到已显示的列表中以阻止它再次出现...

$term_list = wp_get_post_terms($post->ID, 'country', array("all"));
foreach($term_list as $term_single) {
    if ( !in_array( $term_single->slug, $alreadyDisplayed) ) {
        echo '<a href="'.dirname(get_the_permalink()).'">';
        echo $term_single->slug;
        echo '</a><br>';
        $alreadyDisplayed[] =  $term_single->slug;
    }
}

您将需要...

$alreadyDisplayed = [];

行前

while ($random_query->have_posts()) {