duanbai5348 2015-03-04 15:01
浏览 66
已采纳

从子页面的自定义字段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(); ?>
  • 写回答

1条回答 默认 最新

  • duanming7833 2015-03-04 15:11
    关注

    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 ) ); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog