ACF-获取数组的唯一值

时间:2018-08-21 作者:Fellipe Abreu

我需要获得数组的唯一值,以便在过滤器中使用。这是我的密码

<?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>
我该怎么做?我尝试使用array\\u unique(),但没有成功。

谢谢

1 个回复
SO网友:Fellipe Abreu

这样做正确吗?

`<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>`

结束

相关推荐

如何链接到详细信息页面(single.php?)在wp_loop中

我进行了如下wp\\U查询。这是其中的一部分。这是可行的。$args = .....; $query = new WP_Query($args); while($query->have_posts() ) : $query->the_post(); ?> <div class=\"each\"> <h4><?php the_title(); ?> &