如果值相同,如何只显示一次 meta_value

How to display meta_value only once if the value is the same

我创建了一个循环来显示元值,但如果它们的值相同,我只想显示一次。我试过使用 array_unique 但它似乎不起作用

   $query = new WP_Query( $args );    

  
  if ( $query->have_posts() ) {
    echo '<ul>';
    $menusInList = [];
      while ( $_query->have_posts() ) {
        $query->the_post();
        $menu = get_post_meta($post->ID, 'awarded', true);
        if (in_array($menu, $menusInList)) {
        continue;
    }

    $menusInList[] = $menu;
          echo '<li class="'.$menus .'" >' . $menu . '</li>';
        }
          echo '</ul>';
        } else {
          // no posts found
        }
  /* Restore original Post Data */
    wp_reset_postdata();

$menu 保存在数组 $menusInList 中并通过 in_array 检查。如果 returns true 使用 continue 跳过。

$menusInList = [];
while ( $query->have_posts() ) {
    $query->the_post();
    $menu = get_post_meta($post->ID, 'award', true);

    if (in_array($menu, $menusInList)) {
        continue;
    }

    $menusInList[] = $menu;

    // ...

}