0

我在我的网站上使用type_evenement具有 5 个可能值(val_1、val_2、val_3、val_4、val_5)的选择字段 ( )的高级自定义字段

我还在自定义模板页面上使用 wp 查询来显示某个类别的帖子,所有帖子都使用“选择”自定义字段。

我正在尝试在此页面上显示所有选择值,但仅显示一次,因此我尝试使用 array_unique 删除双精度值,但它不起作用。

这是我为显示循环内的值而编写的代码,它显示所有值,即使是双精度值,例如,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 应该工作不?

如果有人可以帮助我,那就太好了!

多谢

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 回答