我在我的网站上使用高级自定义字段,其中包含5个可能值的选择字段( type_evenement )(val_1,val_2,val_3,val_4,val_5)
我在自定义模板页面上也使用wp查询来显示某个类别的帖子,所有帖子都使用“select”自定义字段。
我试图在这个页面上显示所有选择值,但只有一次,所以我试图使用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应该不工作?
如果有人可以帮助我,这将是件好事!
非常感谢
I'm using advanced custom fields on my website with a select field (type_evenement) with 5 possible values (val_1, val_2, val_3, val_4, val_5)
I'm using also a wp query on a custom template page to display posts from a category, all posts uses the "select" custom field.
I'm trying to display all the select values on this page, but only once, so I'm trying to delete the double values, using array_unique, but it's not working.
here is the code I wrote to display the values inside the loop, it display all the values, even when double, for example, 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(); ?>but when using array_unique, nothing is displayed anymore :
<?php $input = get_field('type_evenement'); $result = array_unique($input); echo($result); ?>I don't understand what I'm doing wrong, I know that get_field returns an array, so I guess that array_unique should work no ?
If someone can help me with this would be nice !
thanks a lot
最满意答案
$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. }$input is just a single value, not an array. The duplicates are repeatedly assigned to that value by the while loop.
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $input = get_field('type_evenement'); echo($input);You could either fix the query that returns the duplicate, but since it looks like wordpress, that might not going to work.
So you could fill an array first, then use array_unique() and then echo the values (or you could simple not add a value again, if the temporary array already contains it):
$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. }更多推荐
发布评论