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

我创建了一个循环来显示元值,但如果它们的值相同,则只想显示一次。我尝试过使用 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();


慕慕森
浏览 89回答 1
1回答

白猪掌柜的

保存$menu在数组中$menusInList并通过检查in_array。如果返回 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;    // ...}
打开App,查看更多内容
随时随地看视频慕课网APP