从子页面的自定义字段wordpress循环中删除重复结果
I am looping through all child pages of the current page. I am returning the results of the custom field 'bedrooms'. This is resulting in a list of numbers (numbers of bedrooms) like so - 131413. This is what I expect.
However I want to remove the duplicates so in the example above it will be returned as 134.
I've looked into arrays but and not the best when it comes to php so can anyone help please?
Here's my current code for the child loop and return of acf field.
<?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(); ?>
我循环遍历当前页面的所有子页面。 我将返回自定义字段'卧室'的结果。 这导致了一个数字列表(卧室数量),如此 - 131413.这就是我所期望的。 p>
但是我想删除重复项,所以在上面的示例中它将是 返回134. p>
我已经研究过数组,但是当涉及到php时并不是最好的,所以任何人都可以帮忙吗? p>
这是我的 子循环的当前代码和acf字段的返回。 p>
&lt;?php
$ args = array(
'post_type'=&gt;'property',\ n'posst_per_page'=&gt; -1,
'post_parent'=&gt; $ post-&gt; ID,
'orderby'=&gt;'plot_number',
'order'=&gt;'ASC'
);
$ parent = new WP_Query($ args);
if($ parent-&gt; have_posts()):?&gt;
&lt;?php while($ parent-&gt; have_posts()):$ parent-&gt; the_post(); ?&gt;
&lt;?php the_field('卧室'); ?&gt;
&lt;?php endwhile; ?&gt;
&lt;?php endif; wp_reset_query(); ?&gt;
code> pre>
div>
My suggest would be to put the numbers into an array (an idea you alluded to in your question).
I use implode()
to join the elements of the array using an empty string (no spaces) as the glue. I'm also using the array_unique()
function to return a new array without duplicates.
Also note the use of get_field()
which will return the field value instead of the_field()
which will output it.
Example:
<?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 ) ); ?>