0

type_evenementウェブサイトで高度なカスタム フィールドを使用しており、5 つの可能な値 (val_1、val_2、val_3、val_4、val_5) を持つ選択フィールド ( ) を使用しています。

また、カスタム テンプレート ページで wp クエリを使用してカテゴリから投稿を表示しています。すべての投稿は「選択」カスタム フィールドを使用します。

このページにすべての選択値を表示しようとしていますが、一度しか表示されないため、array_unique を使用して double 値を削除しようとしていますが、機能していません。

これは、ループ内の値を表示するために私が書いたコードです。たとえば、val_1、val_3、val_4、val_2、val_1、val_1...

<?php 

// args
$today = date("Ymd");


$args = array (
'category' => 5,
'posts_per_page' => -1,
'meta_key'       => 'debut_date',
'orderby'       => 'meta_value_num',
'order'          => 'ASC',
'meta_query' => array(
array(
'key'       => 'fin_date',
'compare'   => '>=',
'value'     => $today,
)
),
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>

<?php if( $the_query->have_posts() ): ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <?php
        $input = get_field('type_evenement');
        echo($input);
        ?>


<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

ただし、array_unique を使用すると、何も表示されなくなります。

<?php
$input = get_field('type_evenement');
$result = array_unique($input);
echo($result);
?>

get_field が配列を返すことはわかっているので、array_unique は no で動作するはずです。

誰かがこれで私を助けることができればいいです!

どうもありがとう

4

1 に答える 1

0

$input配列ではなく、単一の値です。複製は、 によってその値に繰り返し割り当てられますwhile loop

<?php while ( $the_query->have_posts() ) : $the_query->the_post();

     $input = get_field('type_evenement');
     echo($input);

重複を返すクエリを修正することもできますが、wordpress のように見えるため、うまくいかない可能性があります。

したがって、最初に配列を埋めてから、値を使用array_unique()してからエコーすることができます(または、一時配列にすでに値が含まれている場合は、単純に値を再度追加しないでください):

$myArr = array();

while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $input = get_field('type_evenement');

    if (!in_array($input,$myArr)){
      $myArr[] = $input;
    }
 }

 foreach ($myArr AS $value){
    echo $value; //unique values.
 }
于 2014-11-12T17:49:19.443 に答える