admin管理员组

文章数量:1122846

I need to get unique values of array to use in a filter. This is my code

<?php 
            $newsArgs = array( 
                'post_type'         => 'sistemas-sanitarios',
                'orderby'           => 'menu_order',
                'order'             => 'ASC',
                'posts_per_page'    => -1,
                'post_parent'       => 0,
                'category'         => 'categoria_sistemas_sanitarios',
                'meta_query'        => array(
                array(
                    'key'           => 'segmento_produto',
                    'value'         => $linhas['id'],
                    'compare'       => 'LIKE',
                    )
                )
                );  
                $filter_query = new WP_Query( $newsArgs );
            ?>


            <ul id="filters" class="clearfix">
                <?php
                $taxonomy = 'categoria_sistemas_sanitarios';
                $terms = get_terms( $taxonomy );
                echo '<li><span class="filter-'. $title . ' active" data-filter="';
                $li = ' ';
                foreach ( $terms as $term) {
                    $li .= '.' . $term->slug.', ';

                }
                $li = rtrim($li, ', ');
                echo $li;
                echo '">Todos os Sistemas</span></li>';
                while ( $filter_query->have_posts() ) : $filter_query->the_post();

                $categoria = get_the_terms( $post->ID, 'categoria_sistemas_sanitarios' );
                $categoriaSlug = $categoria[0]->slug;
                $categoriaName = $categoria[0]->name;

                    echo '<li><span class="filter-'. $title . '" data-filter=".' . $categoriaSlug . '">' . $categoriaName . '</li>';
                endwhile; wp_reset_postdata();
                ?>
            </ul>

How can I do that? I tried to use array_unique() but didn't work.

Thanks!

I need to get unique values of array to use in a filter. This is my code

<?php 
            $newsArgs = array( 
                'post_type'         => 'sistemas-sanitarios',
                'orderby'           => 'menu_order',
                'order'             => 'ASC',
                'posts_per_page'    => -1,
                'post_parent'       => 0,
                'category'         => 'categoria_sistemas_sanitarios',
                'meta_query'        => array(
                array(
                    'key'           => 'segmento_produto',
                    'value'         => $linhas['id'],
                    'compare'       => 'LIKE',
                    )
                )
                );  
                $filter_query = new WP_Query( $newsArgs );
            ?>


            <ul id="filters" class="clearfix">
                <?php
                $taxonomy = 'categoria_sistemas_sanitarios';
                $terms = get_terms( $taxonomy );
                echo '<li><span class="filter-'. $title . ' active" data-filter="';
                $li = ' ';
                foreach ( $terms as $term) {
                    $li .= '.' . $term->slug.', ';

                }
                $li = rtrim($li, ', ');
                echo $li;
                echo '">Todos os Sistemas</span></li>';
                while ( $filter_query->have_posts() ) : $filter_query->the_post();

                $categoria = get_the_terms( $post->ID, 'categoria_sistemas_sanitarios' );
                $categoriaSlug = $categoria[0]->slug;
                $categoriaName = $categoria[0]->name;

                    echo '<li><span class="filter-'. $title . '" data-filter=".' . $categoriaSlug . '">' . $categoriaName . '</li>';
                endwhile; wp_reset_postdata();
                ?>
            </ul>

How can I do that? I tried to use array_unique() but didn't work.

Thanks!

Share Improve this question asked Aug 21, 2018 at 0:26 Fellipe AbreuFellipe Abreu 1
Add a comment  | 

1 Answer 1

Reset to default 0

Is correct to do this way?

`<ul id="filters" class="clearfix">
                <?php
                $taxonomy = 'categoria_sistemas_sanitarios';
                $terms = get_terms( $taxonomy );
                $unique = array();
                echo '<li><span class="filter-'. $title . ' active" data-filter="';
                $li = ' ';
                foreach ( $terms as $term) {
                    $li .= '.' . $term->slug.', ';

                }
                $li = rtrim($li, ', ');
                echo $li;
                echo '">Todos os Sistemas</span></li>';
                while ( $filter_query->have_posts() ) : $filter_query->the_post();

                $categoria = get_the_terms( $post->ID, 'categoria_sistemas_sanitarios' );
                $categoriaSlug = $categoria[0]->slug;
                $categoriaName = $categoria[0]->name;

                if( ! in_array( $categoriaSlug, $unique ) ) :
                    $unique[0] = $categoriaSlug;

                    echo '<li><span class="filter-'. $title . '" data-filter=".' . $unique[0] . '">' . $unique[0] . '</li>';
                endif;
                endwhile; wp_reset_postdata();
                ?>
            </ul>`

本文标签: custom taxonomyACFGet unique values of array