如何在循环中使用woocommerce数据属性 [英] How to use woocommerce data attributes in the loop

查看:38
本文介绍了如何在循环中使用woocommerce数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于列出 woocommerce 产品的自定义产品类型和自定义循环

I have a custom product type and custom loop for listing woocommerce products

$query_args = array(
 'post_type' => 'product',
 'tax_query' => array(
      array(
          'taxonomy' => 'product_type',
          'field'    => 'slug',
          'terms'    => 'custom_type', 
      ),
  ),
);

$r = new WP_Query( $query_args );

if ( $r->have_posts() ) {

我在产品数据中有自定义数据属性.我如何在循环中使用它们?如何过滤具有此属性的产品?

I have custom data attributes in product datas. How can I use them in loop? How can I filter products with this attributes?

例如我有颜色和尺寸数据属性.现在我怎样才能列出红色和大的产品?

For example I have color and size data attributes. Now how can I the list red and large products?

推荐答案

属性只是自定义分类法.请记住,分类名称将始终是以 pa_ 开头的属性名称.这只是 WooCommerce 的命名约定,以避免冲突的分类名称.要查询多个分类,请参阅 WP 查询参数中的多分类处理"部分.

Attributes are just custom taxonomies. Keep in mind that the taxonomy name will always be the attribute name preceded by pa_. This is just WooCommerce's naming convention to avoid conflicting taxonomy names. To query more than one taxonomy, see the "Multiple Taxonomy Handling" section in WP Query Parameters.

例如,如果您尝试查询产品类型 = custom_type 和颜色 = red 尺寸属性 = large,您的示例参数将如下所示:

If, for example, you were trying to query products of product type = custom_type and color = red and a size attribute = large, your example args would look like this:

$query_args = array(
 'post_type' => 'product',
 'tax_query' => array(
      array(
          'taxonomy' => 'product_type',
          'field'    => 'slug',
          'terms'    => 'custom_type', 
      ),
      array(
          'taxonomy' => 'pa_color',
          'field'    => 'slug',
          'terms'    => 'red', 
      ),
      array(
          'taxonomy' => 'pa_size',
          'field'    => 'slug',
          'terms'    => 'large', 
      ),
  ),
);

这篇关于如何在循环中使用woocommerce数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆