duanluo9369 2017-11-24 12:13 采纳率: 100%
浏览 58
已采纳

从WP_Query for WooCommerce产品中的foreach循环中排除重复项

I have a WP Query to list matching products, like this:

function sku_ean_sizes () {
global $product;

$current_stijlcode = $product->get_attribute( 'pa_stijlcode' );
$current_ean = $product->get_attribute( 'pa_ean' );

$postid = get_the_ID();

    $args = array(
      'post_type' => 'product',
      'orderby' => 'meta_value_num',
        'meta_key' => '_price',
        'order' => 'asc',
      'posts_per_page' => 100,
      'tax_query' => array(
    'relation' => 'OR',
    array(
      'taxonomy' => 'pa_stijlcode',
      'field'    => 'slug',
      'terms'    => $current_stijlcode,
    ),
    array(
      'taxonomy' => 'pa_ean',
      'field'    => 'slug',
      'terms'    => $current_ean,
    )
  )
      );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
      while ( $loop->have_posts() ) : $loop->the_post();



global $product;

foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
  echo '<span>' . $attribute_value . '</span>';
}

      endwhile;
    } else {
      // no sku matches found
    }
    wp_reset_postdata();
}

I would like to use the foreach loop to list the found product attributes attached to the products found with the WP Query:

global $product;

    foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
      echo '<span>' . $attribute_value . '</span>';
    }

This code works. However, the $attribute_value variable outputs duplicate records. This makes sense, as multiple products could have the same output.

What adjustment can i make so it excludes duplicate values?

In context; This is to display all available sizes for a specific product.

  • 写回答

1条回答 默认 最新

  • duanpang2751 2017-11-24 12:34
    关注

    Updated

    You should use a foreach loop to set all the values in an array avoiding duplicates

    Then, using implode() PHP function, you will display all attributes term names without duplicates.

    This partial code to replace yours, will display the non duplicated attribute term names for all products at once:

    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ):
        $maat_term_names = array();
        while ( $loop->have_posts() ): 
            $loop->the_post();
    
            // Set the attribute term names in an array avoiding duplicates
            foreach( wc_get_product_terms( $loop->post->ID, 'pa_maat' ) as $attribute_value ):
                $maat_term_names[$attribute_value] = $attribute_value;
            endforeach;
        endwhile;
    
        // Sorting (ordering)
        sort($maat_term_names);
    
        // Here you display attribute term names without duplicates (coma separated)
        echo '<span>' . implode( '</span>, <span>', $maat_term_names ) . '</span> ';
    
    else:
       echo '<span>No SKUs matches found</span>';
    endif;
    
    wp_reset_postdata();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思