我终于解决了这个问题。我之所以复制代码,是因为它可能会帮助某些人:)我通过填充arrray并检查品牌是否存在,找到了解决方案,因此我只获得每个品牌一次。
<?php
//Query to match department
$args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'department\',
\'field\' => \'slug\',
\'terms\' => array( \'department_name\' )
),
),
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'post_type\' => \'prod\', \'posts_per_page\' => \'-1\', \'posts_per_page\' => \'-1\'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
// Array of brands
$brands= array();
$term = $query->queried_object;
while ( $query->have_posts() ) : $query->the_post(); ?>
<!-- Loop to show the brand -->
<?php
$terms = get_the_terms( $post->ID , \'brabds\' );
// Loop por el array de marcas
if ( $terms != null ){
foreach( $terms as $term ) {
// I want to check if my brand has only one product or more
// Only one product
if ($term ->count == 1){
// I create a variable so I can check if it\'s in my brands array
$marca = $term->name;
// If is not in array, push it there
if ( ! in_array($marca, $marcas)){
array_push($marcas, $marca);
// This query is to link to a product and get some info
$tax_query = \'\';
$tax_query[] = array(\'taxonomy\' => \'brands\',\'field\' => \'term_id\',\'terms\' => $term->term_id);
$term_post = get_posts(array(\'post_type\' => \'prod\',\'tax_query\' => $tax_query));
if (!empty($term_post)) {
$term_post_link = get_permalink($term_post[0]->ID);
$id_prod = url_to_postid($term_post_link);
$nombre_prod = get_the_title($term_post[0]->ID);
echo \'<a title="\'.$term->name.\'" rel="\'.$id_prod.\'">\'.$term->name.\'</a>\';
}
}
}
// Marcas MULTI
else{
// If there are more than a product, do the same thing but query as my needs
$marca = $term->name;
if ( ! in_array($marca, $marcas)){
array_push($marcas, $marca);
echo \'<a rel="\'.$term->term_id.\'" title="\'.$term->name.\'">\'.$term->name.\'</a>\';
}
}
}
}
// End loop
?>
<?php
endwhile;
wp_reset_query();
} ?>