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

报告相同问题?