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

报告相同问题?

悬赏问题

  • ¥30 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52