我正在遍历当前页面的所有子页面。我将返回自定义字段'卧室'的结果。这导致了一个数字列表(卧室数量),如此 - 131413.这就是我所期望的。
但是我想删除重复项,因此在上面的例子中它将返回134.
我已经研究了数组,但是当涉及到php时并不是最好的,所以任何人都可以帮忙吗?
这是我当前的子循环代码和acf字段的返回。
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'orderby' => 'plot_number',
'order' => 'ASC'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php the_field('bedrooms'); ?>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
答案 0 :(得分:2)
我的建议是将数字放入数组(你在问题中提到的一个想法)。
我使用implode()
使用空字符串(无空格)作为粘合来连接数组的元素。我还使用array_unique()
函数返回一个没有重复的新数组。
另请注意使用get_field()
将返回字段值,而不是the_field()
,这将输出它。
示例:
<?php
$bedrooms = array();
while ( $parent->have_posts() ) : $parent->the_post();
// Add 'bedrooms' field value to the array.
$bedrooms[] = get_field( 'bedrooms' );
endwhile;
// Output as string with no spaces and duplicates removed.
echo implode( '', array_unique( $bedrooms ) ); ?>